0
点赞
收藏
分享

微信扫一扫

蓝桥杯Java——算法训练4

1.P0704(回文数和质数)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int min = sc.nextInt();
        int max = sc.nextInt();
 
        //判断质数
        for (int i = min; i <= max; i++) {
            if (i > 1) {
                boolean bool = true;
                for (int j = 2; j <= Math.sqrt(i); j++)
                    if (i % j == 0) {
                        bool = false;
                        break;
                    }
                if (bool) {
                    //判断回文数
                    String a = String.valueOf(i);
                    char[] c = a.toCharArray();
                    int front = 0;
                    int after = a.length() - 1;
                    while (front < after) {
                        if (c[front] != c[after]) {
                            bool = false;
                            break;
                        }
                        front++;
                        after--;
                    }
 
                }
                if (bool) System.out.print(i + " ");
            }
        }
    }
}

 

举报

相关推荐

0 条评论