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;
    }
}


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

相关推荐

暴杀流调参工作者:春招又试了一些岗位,现在投递很有意思,不仅要精心准备简历,投递官网还得把自己写的东西一条一条复制上去,阿里更是各个bu都有自己的官网,重复操作无数次,投完简历卡完学历了,又该写性格测评、能力测评,写完了又要写专业笔试,最近还有些公司搞了AI辅助编程笔试,有些还有AI面试,对着机器人话也听不明白录屏硬说,终于到了人工面试又要一二三四面,小组成员面主管面部门主管面hr面,次次都没出错机会,稍有不慎就是挂。 卡学历卡项目卡论文卡实习什么都卡,没有不卡的😂
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务