66分求助

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

xuzihao123 @ 2023-07-21 09:19:32

#include <iostream>
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int gu(int i){
        string s;
        stringstream ss;
        ss<<i;
        ss>>s;
        int l,r;
        l=0;
        r=s.size()-1;
        while(l<r){
            if(s[l]!=s[r])return 0;
            l++;
            r--;
        }
        return 1;
}
int su(int i){
    if(i==0||i==1)return 0;
    if(i==2)return 1;
    for(int k=2;k*k<=i;k++)if(i%k==0)return 0;
    return 1;
}
int main(){
    int l,r;
    int a,b;
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(i%2==0)continue;
        if(gu(i))if(su(i))cout<<i<<endl;
    }
}

by lijuncheng_1207 @ 2023-07-27 17:59:11

@xuzihao123 我与你一样

#include <bits/stdc++.h>
using namespace std;
int n,m,cnt;
bool prime(int x){
    for(int i=2;i<=sqrt(x);i++) {
        if(x%i==0)
            return 0;
    }
    return 1;
}
bool flag(int x){
    string s=to_string(x);
    string s1=s;
    reverse(s.begin(),s.end());
    if(s1==s){
        return 1;
    }
    return 0;
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0); 
    cin>>n>>m;
    for(int i=n;i<=m;i++) {
        if(prime(i)&&flag(i)){
            cout<<i<<endl;
        }
    }
    return 0;
}

|