2021 京东校招回文素数Java解

import java.util.*;
public class Test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println(helper(in.nextInt(), in.nextInt()));
    }    public static int helper(int l, int r) {
        int count = 0;
        while (l < r) {
            String str = String.valueOf(l);
            for (int i = 0; i < str.length(); i++) {
                int ans = toDelOne(str, i);
                //注意1的判断
                if (l != 1 && isHuiWen(l) && isSu(l)) {
                    count++;
                    System.out.println(l);
                    break;
                } else if (ans > 1 && isHuiWen(ans) && isSu(ans)) {
                    count++;
                    System.out.println(ans);
                    break;
                }
            }
            l++;
        }
        return count;
    }
    //删除str的第i位
    private static int toDelOne(String str, int i) {
        int num = Integer.parseInt(str);
        int ans = (int) ((int) (num/Math.pow(10,i +1 ))*Math.pow(10,i  ) + num%Math.pow(10,i  ));
        return ans;
    }
    //判断素数
    public static boolean isSu(int num){
        int i = 2;
        while(i * i <= num){
            int j = num / i;
            if(j * i  == num) {
                return false;
            }
            i++;
        }
        return true;
    }
    // 判断回文
    public static boolean isHuiWen(int num){
        char[] cs = Integer.toString(num).toCharArray();
        int i = 0,j = cs.length - 1;
        while(i <= j){
            if(cs[i] != cs[j]) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}


#京东##笔试题目#
全部评论

相关推荐

部门氛围好,不卷,欢迎投递!
投递蚂蚁集团等公司10个岗位 >
点赞 评论 收藏
转发
点赞 1 评论
分享
牛客网
牛客企业服务