求助

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

WiseStar @ 2023-07-15 11:53:04

怎么测试点全错啊,我用的是书上的代码 (基础,62-63页的代码)

#include<iostream>
using namespace std;
int main(){
    int a,b,num,flag=1;
    cin>>a>>b;
    if(a<=5&&b>=5)
        cout<<5<<endl;
    if(a<=7&&a>=7)
        cout<<7<<endl;
    if(a<=11&&b>=11)
        cout<<11<<endl;
    for(int i=1;i<=9;i+=2)
        for(int j=0;j<=9;j++){
            num=100*i+10*j+i;
            if(num<a)
                continue;
            if(num>b)
                return 0;
            for(int j=3;j*j<=num;j++)
                if(num%j==0){
                    flag=0;
                    break;
                }
            if(flag)
                cout<<num<<endl;
        }
    for(int i=1;i<=9;i+=2)
        for(int j=0;j<=9;j++)
        for(int k=0;k<=9;k++){
            num=10000*i+1000*j+100*k+10*j+i;
            if(num<a)
                continue;
            if(num>b)
                return 0;
            flag=1;
            for(int j=3;j*j<=num;j++)
                if(num%j==0){
                    flag=0;
                    break;
                }
            if(flag)
                cout<<num<<endl;
        }
    for(int i=1;i<=9;i+=2)
        for(int j=0;j<=9;j++)
        for(int k=0;k<=9;k++)
        for(int i0=0;i0<=9;i++){
            num=1000000*i+100000*j+10000*k+1000*i0+100*k+10*j+i;
            if(num<a)
                continue;
            if(num>b)
                return 0;
            flag=1;
            for(int j=3;j*j<=num;j++)
                if(num%j==0){
                    flag=0;
                    break;
                }
            if(flag)
                cout<<num<<endl;
        }
    return 0;
}

对了,本人蒟蒻


by WiseStar @ 2023-07-15 11:53:36

说一句,没用数组


by FincheuwYggdrasil @ 2023-07-15 12:40:44

为什么不自己看思路写呢?


by WiseStar @ 2023-07-15 15:33:46

@FincheuwYggdrasil 先自己写,后来实在没办法,看的书。可还是全错(书上居然没用数组)


by ltzx2022_kanxinyi_5 @ 2023-07-17 16:16:13

#include<bits/stdc++.h>
using namespace std;
int i,n,sum,m;
bool check(int a){
    for(int i=2;i*i<=a;i++)
    {
        if(a%i==0)
        return false;
    }
    return true;
}
int hw(int n) { 
    int sum=0;
    int k=n;
    while(n!=0) {
        sum=sum*10+n%10;
        n/=10;
    }
    if(sum==k)
        return 1;
    else
        return 0;
}
int main()
{ 
        cin>>n>>m;
        for(i=n; i<=m; i++) {
            if(i==9989900)
                break;
            if(hw(i)&&check(i))
                cout<<i<<endl; 
        }
    return 0; 
} 

by zengziyuezzy @ 2023-07-17 19:42:27

@FincheuwYggdrasil +1


|