只有33分,TLE了

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

ljt0203 @ 2023-06-05 23:50:13

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a=scanner.nextInt(),b=scanner.nextInt();
        scanner.close();
        int[] a1 =new int[5];
        int[] a2 =new int[50];
        int[] a3 =new int[50];
        int[] a4 =new int[500];
        int[] a5 =new int[500];
        int[] a6 =new int[5000];
        int[] a7 =new int[5000];
        int[] a8 =new int[50000];
        int d1,d2,d3,d4,d5;
        int p1,p2,p3,p4,p5,p6,p7,p8;
        int i1=0,i2=0,i3=0,i4=0,i5=0;
        for (d1=1;d1<=9;d1+=2) {
            p1=d1*10+d1;
            a1[i1]=p1;
            i1++;
            for (d2=0;d2<=9;d2++) {
                p2=d1*100+d2*10+d1;
                p3=d1*1000+d2*100+d2*10+d1;
                a2[i2]=p2;
                a3[i2]=p3;
                i2++;
                for (d3=0;d3<=9;d3++) {
                    p4=10000*d1+1000*d2+100*d3+10*d2+d1;
                    p5=100000*d1+10000*d2+1000*d3+100*d3+10*d2+d1;
                    a5[i3]=p5;
                    a4[i3]=p4;
                    i3++;
                    for (d4=0;d4<=9;d4++){
                        p6=1000000*d1+100000*d2+10000*d3+1000*d4+100*d3+10*d2+d1;
                        p7=10000000*d1+1000000*d2+100000*d3+10000*d4+1000*d4+100*d3+10*d2+d1;
                        a7[i4]=p7;
                        a6[i4]=p6;
                        i4++;
                        for (d5=0;d5<=9;d5++){
                            p8=100000000*d1+10000000*d2+1000000*d3+100000*d4+d5*10000+1000*d4+100*d3+10*d2+d1;
                            a8[i5]=p8;
                            i5++;
                        }
                    }
                }
            }
        }
        for (int num=a;num<b;num++){
            boolean c=true;
            for (int n=2;n<num;n++){
                if (num%n==0){
                    c=false;
                    break;
                }
            }
            if (c){
                if(num<10){
                    System.out.println(num);
                }else {
                    if (num<100){
                        for (int j : a1) {
                            if (num == j) {
                                System.out.println(num);
                            }
                        }
                    }else {
                        if (num<1000){
                            for (int j : a2) {
                                if (num == j) {
                                    System.out.println(num);
                                }
                            }
                        }else {
                            if (num<10000){
                                for (int j : a3) {
                                    if (num == j) {
                                        System.out.println(num);
                                    }
                                }
                            }else {
                                if (num<100000){
                                    for (int j : a4) {
                                        if (num == j) {
                                            System.out.println(num);
                                        }
                                    }
                                }else {
                                    if (num<1000000){
                                        for (int j : a5) {
                                            if (num == j) {
                                                System.out.println(num);
                                            }
                                        }
                                    }else {
                                        if(num<10000000){
                                            for (int j : a6) {
                                                if (num == j) {
                                                    System.out.println(num);
                                                }
                                            }
                                        }else {
                                            if (num<100000000){
                                                for (int j : a7) {
                                                    if (num == j) {
                                                        System.out.println(num);
                                                    }
                                                }
                                            }else {
                                                for (int j : a8) {
                                                    if (num == j) {
                                                        System.out.println(num);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

by qwiwiiwjjdks @ 2023-07-02 14:25:59

用c++更好

#include<bits/stdc++.h>
using namespace std;
bool pdzs(int n){
    if(n<2) return false;
    for(int i=2;i*i<=n;i++){
        if(n%i==0) return false;
    }
    return true;
}
bool h(int n){
    int y = 0,k = n;
    while(n>0){
        y = y*10+n%10;
        n = n/10;
    }
    if(y==k) return true;
    else return false;
} 
int main(){
    int a,b;
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(i>9989899) return 0;
        else{
            if(i!=2 && i%2==0) continue;
            else if(h(i)){
            if(pdzs(i)) printf("%d\n",i);
            }
            else continue;
        }

    }
    return 0;
}

|