多年不练截取string用substr都忘了,还有要记住最后是要输出整一条string,比如,应该0023,而不是23,一开始一直不对。。。 #include <iostream> #include <stdlib.h> #include <string> #include<algorithm> #include<vector> #include<cmath> using namespace std; int isprime(int k) { int result = 0; if (k == 1) { result = 1; } for (int i = 2; i < sqrt(k); i++) { if (k%i == 0) { result = 1; break; } } return result; } int main() { int l, n; string s; cin >> l >> n; cin >> s; string result = "404"; for (int i = 0; i < l - n + 1; i++) { string k = s.substr(i, n); int t = stoi(k); if (isprime(t) == 0) { result = k; break; } } cout << result; system("pause"); };
原文地址:https://www.cnblogs.com/wsggb123/p/10205073.html
时间: 2024-10-20 18:34:21