链接
给定长度为L的字符串,求第一个长度为k的子串,使之为素数
#include<bits/stdc++.h>
using namespace std;
bool isprime(string s){
int d = stoi(s);
if(d == 1 || d == 0) return false;
if(d == 2) return true;
for(int i=2;i<=(int)sqrt(d);i++){
if(d % i == 0) return false;
}
return true;
}
int main(){
int n,m;
string s;
cin>>n>>m;
cin>>s;
int len = s.length();
bool ans = 0;
for(int i=0; i<len; i++){
if(i+m-1 >= len) break;
string t = s.substr(i, m);
if(isprime(t)){
cout<<t<<endl;
ans = 1;
break;
}
}
if(!ans) cout<<"404"<<endl;
}
PAT A1152 Google Recruitment [素数+字符串处理]
原文:https://www.cnblogs.com/doragd/p/11449028.html