1152

素数判断,暴力搜索

string.substr(start,len); //参数分别是开始位置,和截取字串长度。

数字转字符串   to_string(int value);

字符串转数字   stoi(string str);

                        stol(string str);

                        stof(string str);

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
bool IsPrime(int a);
int main()
{
	int n, k;
	string s;
	cin >> n >> k;
	cin >> s;
	int l = s.length();
	bool flag = false;
	for (int i = 0; i+k-1 < l; i++)
	{
		string str;
		int a;
		str = s.substr(i, k);
		a = stoi(str);
		if (a > 0)
		{
			if (IsPrime(a))
			{
				flag = true;
				cout << str << endl;
				break;
			}
		}
		
	}
	if (!flag)
		cout << "404" << endl;
	system("pause");
	return 0;
}
bool IsPrime(int a)
{
	for (int i = 2; i <= sqrt(a); i++)
	{
		if (a%i == 0)
			return false;
	}
	return true;
}
发布了195 篇原创文章 · 获赞 9 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/zero_1778393206/article/details/87978896