最后三个超时,跪求大佬们帮助

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

zhu5001210249 @ 2024-02-21 22:10:48


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        for (int i = a; i <= b; i++) {
            int temp = i;
            int result = 0;

            int ans=huiWen(i);
            if(ans==temp) {

                boolean flag = true;
                for (int j = 2; j < temp; j++) {
                    if (temp % j == 0) {
                        flag = false;
                        break;
                    }
                }

                if(flag){
                    System.out.println(temp);
                }
            }
        }
    }
    public static int huiWen(int i) {
        int result=0;
        while (i != 0) {
            int ge = i % 10;
            result = result * 10 + ge;
            i /= 10;
        }
        return result;
    }
}

|