我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC
我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED
原题链接:
PAT-BASIC1094:https://pintia.cn/problem-sets/994805260223102976/problems/1071785997033074688
PAT-ADVANCED1152:https://pintia.cn/problem-sets/994805342720868352/problems/1071785055080476672
题目描述:
PAT-BASIC1094:
PAT-ADVANCED1152:
知识点:素数、字符串
由于我们不确定该数字有没有前导0,有多少个前导0,因此需要用字符串的形式输出结果。
时间复杂度和输入的字符串有关。空间复杂度是O(1)。
C++代码:
#include
#include
#include
using namespace std;
int L, K;
bool isPrime(int num);
int main(){
scanf("%d %d", &L, &K);
char input[L + 1];
scanf("%s", input);
if(K == 0){
printf("404\n");
return 0;
}
for(int i = 0; i + K <= strlen(input); i++){
int result = 0;
char temp[K + 1];
for(int j = i; j < i + K; j++){
result = input[j] - '0' + result * 10;
temp[j - i] = input[j];
}
temp[K] = '\0';
if(isPrime(result)){
printf("%s\n", temp);
return 0;
}
}
printf("404\n");
return 0;
}
bool isPrime(int num){
if(num == 1){
return false;
}
for(int i = 2; i <= sqrt(num); i++){
if(num % i == 0){
return false;
}
}
return true;
}
C++解题报告: