88分求助

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

huyihang @ 2024-08-17 20:39:51

#include<bits/stdc++.h>
using namespace std;
const int N=1e9+10;
long long cnt=0,p[N/10],zs[N/10];
void shsu(int n)
{
    for(int i=2;i<=n;i++){
        if(p[i]==0)zs[++cnt]=i;
        for(int j=1;j<=cnt;j++){
            long long m=zs[j]*i;
            if(m>n)break;
            p[m]=1;
            if(i%zs[j]==0)break;
        }
    }
}
bool ishw(long long a)
{
    long long s=0,n,y=a;
    while(a>0){
        n=a%10;
        s=s*10+n;
        a=a/10;
    }
    if(s==y)return 1;
    else return 0;
}
int main()
{
    long long a,b;
    cin>>a>>b;
    shsu(b+100);
    for(int i=1;i<=cnt;i++){
        if(zs[i]<a)continue;
        if(zs[i]>b)break;
        if(ishw(zs[i]))cout<<zs[i]<<endl;
    }
    return 0;
}

by Emil_ @ 2024-08-17 20:42:27

@huyihang ac代码:先判回文,再判质数

#include<bits/stdc++.h>
using namespace std;
bool huiwen(int n){
    bool r=true;
    int w[15]={};
    int k=1;
    while(n>0){
        w[k]=n%10;
        k++;
        n/=10;
    }
    for(int i=1;i<=k;i++){
        if(w[i]!=w[k-i]){
            r=false;
        }
    }
    return r;
}
int main(){
    std::ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0); 
    int a,b;
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(i%2==0){
            i++;
        }
        if(huiwen(i)==true){
            bool f=true;
            for(int j=2;j<=sqrt(i);j++){
                if(i%j==0)
                    f=false;
            }
            if(f)
                cout<<i<<endl;
        }
    }
    return 0;
}

求关


by lccjsw @ 2024-08-17 20:45:53

开一个万能头就可以了:AC(求关)

#include<bits/stdc++.h>
using namespace std;

bool su(int n){
    if (n<=1) return false;
    for (int i=2;i<=sqrt(n);i++) if (n%i==0) return false;
    return true;
}

bool hui(int n){
    int a=n,b=0;
    while(a){
        b*=10;
        b+=a%10;
        a=a/10;
    }
    return b==n;
}

int main(){
    int n,m;
    cin>>n>>m;
    for (int i=n;i<=m;i++){
        if (hui(i)) {
            if (su(i)) cout<<i<<endl;
            if (i==9989899) return 0;
        }
    }
}

by Emil_ @ 2024-08-17 20:54:57

@huyihang 除了2以外的偶数一定不是素数


by huyihang @ 2024-08-17 21:06:30

@Emil_ 谢谢


|