其他分享
首页 > 其他分享> > PAT(Advanced Level)1152. Google Recruitment

PAT(Advanced Level)1152. Google Recruitment

作者:互联网

转:

PAT(Advanced Level)1152. Google Recruitment

题意

在一个字符串中,找到第一个长度为k的子串,而且这个子串对应的数字要是素数

思路

代码

#include
#include
#include
#include
#include
using namespace std;
bool is_prime(int x) {
    if(x < 2) {
        return false;
    }
    if(x == 2) {
        return true;
    }
    for(int i = 2; i < sqrt(x) + 1; i++) {
        if(x % i == 0) {
            return false;
        }
    }
    return true;
}
int main() {
    string s;
    int l, k;
    cin >> l >> k;
    cin >> s;
    for(int i = 0; i < s.size() - k + 1; i++) {
        int value = stoi(s.substr(i, k));
        if(is_prime(value)) {
            cout << s.substr(i, k);
            return 0;
        }
    }
    cout << 404;
}

转:

PAT(Advanced Level)1152. Google Recruitment

标签:Google,PAT,Level,int,return,include,Advanced
来源: https://www.cnblogs.com/wangtcc/p/14758210.html