PAT甲级1152 Google Recruitment (20分)

2018年12月甲级真题的第一题,如果用对方法就没有坑的初级题目。
题目给的是大数字,用字符串直接处理就行,我一遍就过了。

#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<cctype>
using namespace std;
int main(){
    int l,n,k;
    cin>>l>>n;
    string s,t;
    cin>>s;
    for(int i=0;i<l-n+1;i++){
        k=stoi(s.substr(i,n));
        if(k<2)continue;
        int x=(int)sqrt(k*1.0);
        bool f=true;
        for(int j=2;j<=x;j++){
            if(k%j==0)f=false;
        }
        if(f){
            cout<<s.substr(i,n)<<endl;
            return 0;
        }
    }
    cout<<"404";
    return 0;
}

发布了21 篇原创文章 · 获赞 1 · 访问量 1185

猜你喜欢

转载自blog.csdn.net/allisonshing/article/details/103959255