55分求助,没有超时。

P1217 [USACO1.5] 回文质数 Prime Palindromes

chi_li_zi @ 2024-07-12 11:34:13

#include<bits/stdc++.h>
using namespace std;
int a,b;
string c,fc="";
set<int> p;
bool is_hui_wen(int num){
    string s1=to_string(num);
    string s2=s1;
    reverse(s1.begin(),s1.end());
    return s1==s2;
}
bool is_zhi_shu(int num){
    bool m=1;
    for(int i=2;i<sqrt(num-1);i++){
        if(num%i==0){
            m=0;
            break;
        }
    }
    return m;
}
int main(){
    cin>>a>>b;
    if(a==2)    cout<<"2\n"; 
    if(a%2==0)  a++;
    int max=0;
    b=min(b,9989900);
    for(int i=a;i<=b;i++){
        if(i%2==0) continue;
        if(is_hui_wen(i)&&is_zhi_shu(i)){
            cout<<i<<"\n";
        }
    }
    return 0;
}

by LRRabcd @ 2024-07-12 11:41:03

#include<bits/stdc++.h>
using namespace std;
int a,b;
string c,fc="";
set<int> p;
bool is_hui_wen(int num){
    string s1=to_string(num);
    string s2=s1;
    reverse(s1.begin(),s1.end());
    return s1==s2;
}
bool is_zhi_shu(int num){
    bool m=1;
    for(int i=2;i<=sqrt(num);i++){
        if(num%i==0){
            m=0;
            break;
        }
    }
    return m;
}
int main(){
    cin>>a>>b;
    if(a==2)    cout<<"2\n"; 
    if(a%2==0)  a++;
    int max=0;
    b=min(b,9989900);
    for(int i=a;i<=b;i++){
        if(i%2==0) continue;
        if(is_hui_wen(i)&&is_zhi_shu(i)){
            cout<<i<<"\n";
        }
    }
    return 0;
}

by LRRabcd @ 2024-07-12 11:42:26

质数判断写错了,for循环应该是for(int i=2;i<=sqrt(x);i++)


by chi_li_zi @ 2024-07-12 12:59:37

@20121028LRR 已过,感谢大佬


|