首页 > 京东8月6日Java开发笔试
头像
宮小龙
编辑于 2020-08-07 14:30
+ 关注

京东8月6日Java开发笔试

1.数组求和
package Test.Jd2;

import java.util.Scanner;

public class jd1 {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        double sum = 0;
        for(double i = 1; i <= n; i++ ){
            sum += (double) 1/((double)5 * ((double)2 * i - (double)1)) - (double)1/((double)5 * ((double)2 * i));
        }
        String result =  String.format("%.4f", sum);
        System.out.println(result);
    }
}
2.回文素数
package Test.Jd2;

import java.util.Scanner;

public class jd2 {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        int count = 0;
        for(int i = m; i <= n; i++){
            String s = String.valueOf(i);
            for(int j = s.length() - 1; j >= 0; j--){
                String temp = s.substring(0,j) + s.substring(j+1,s.length());
                int a = Integer.parseInt(temp);
                //System.out.println(a);
                if(isPalindrome(a)){
                    if(isPrime(a)){
                        count++;
                        break;
                    }
                }
            }
        }
        System.out.println(count);
        //System.out.println(isPrime(1));

    }
    public static boolean isPalindrome(int x) {
        char[] chars=String.valueOf(x).toCharArray();
        int left=0;
        int right=chars.length-1;
        while (left<=right){
            if(chars[left++]!=chars[right--]){
                return false;
            }
        }
        return true;
    }
    //判断是不是素数
    public static boolean isPrime(int x) {
        int i = 2;
        for(;i<x;i++) {
            if (x % i == 0) {
                break;
            }
        }
        if(x == i){
            return true;
        }
        return false;
    }

}


全部评论

(1) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐