对于这道题来讲首先读懂题意
要求输出的是k位的素数,而不是连续k位每一位数都是素数的数
我就是因为没有读懂题意从而耽误了很长时间
还有以下两种判断素数的函数,其中有一个是错的大家可以思考一下
显然第一种函数是错的,有很多人可能一样掉进这个坑里
这个else表示遇见一个不整除就说明是素数,但是我们要满足所有的i都不能整除
还有就是两个函数都需要再另行判断n==2这种情况
#include<iostream>
#include<stdlib.h>
#include<string>
#include<math.h>
#include<sstream>
using namespace std;
int su(int t)
{
if (t == 0 || t == 1) {
return 1;
}
if(t==2){
return 0;
}
for (int i = 2; i < sqrt(t); i++) {
if (t % i == 0)
{
return 1;
}
}
return 0;
}
int main()
{
int n, k;
string s;
int y =1;
cin >> n >> k;
cin >> s;
for (int j = 0; j < (n - k + 1); j++)
{
string m=s.substr(j, k);
int x = atoi(m.c_str());
y = su(x);
if (y == 0) {
cout << m << endl;
break;
}
}
if (y==1)
{
cout<<"404" << endl;
}
}