PATA1152 题解
题意分析:
给定一个数字字符串(我都不知道这个L输入有什么用。。。),在字符串中任意选取一个长度为k的字串,判断它是否为可能的素数。如果有,请输出第一个这样的字串;若没有,输出404
注意的点:
这里要注意,这道题需要把0给输出,所以遇到0我们不能直接continue,而是要更新字符串,然后再进行筛选。
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
int length,k;
string str,ans;
bool exist = false;
ll str_to_ll(const string &str){
ll ans = 0;
for(int i = 0;i<str.length();i++){
ans = ans * 10 + str[i] - '0';
}
return ans;
}
bool isPrime(ll num){
if(num <= 1) return false;
int sqr = (ll)sqrt(num);
for(int i = 2;i<=sqr;i++){
if(num % i == 0){
return false;
}
}
return true;
}
int main(){
cin>>length>>k>>str;
for(int i = 0;i<length-k+1;i++){
//if(str[i] == '0') continue;
ans = str.substr(i,k);
int num = str_to_ll(ans);
if(isPrime(num)){
exist = true;
break;
}
}
if(exist){
cout<<ans<<endl;
}
else{
cout<<"404"<<endl;
}
return 0;
}
总结:
水题,没啥好说的。难度和输入输出差不了太多。