8.6京东笔试Java研发全A
第一题 求和
#笔试题目##京东#
// public static double fun(int n){ // double res = 0; // int flag= 1; // for (int i = 1; i <=2*n ; i++) { // res += 1/(5.0*i)*flag; // flag*=-1; // } // BigDecimal b = new BigDecimal(res); // return b.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue(); // } // public static void main(String[] args) { // Scanner in = new Scanner(System.in); // int n = in.nextInt(); // double res = fun(n); // System.out.printf("%.4f",res); // }
第二题 回文素数
import java.util.*; import java.lang.*; public class Main { final static int maxn = 1000000 + 10; static boolean[] isp = new boolean[maxn]; static int[] pow = new int[10]; static void init(){ for (int i = 0; i < maxn; i ++){ isp[i] = true; } isp[0] = false; isp[1] = false; for (int i = 2; i < maxn; i ++){ if (isp[i] == true){ for (int j = i+i; j < maxn; j += i){ isp[j] = false; } } } pow[0] = 1; for (int i = 1; i < 8; i ++){ pow[i] = pow[i-1] * 10; } } static int solve(int i, int j){ int ans = 0; ans += i % pow[j]; ans += i / pow[j+1] * pow[j]; //System.out.println("ans >>> " + ans); return ans; } static boolean isr(String num){ boolean res = true; int length = num.length(); for (int i = 0; i < length/2; i ++){ if( num.charAt(i) != num.charAt(length-i-1) ){ res = false; break; } } return res; } public static void main(String[] args) { // write your code here init(); /* for (int i = 0; i < 1000; i ++){ //if (isp[i] == true){ if (isr(String.valueOf(i))){ System.out.println(i); } } */ int n, m; Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); int ans = 0; for (int i = n; i <= m; i ++){ int length = String.valueOf(i).length(); for (int j = 0; j < length; j ++){ int buf = solve(i, j); if (isp[buf] && isr(String.valueOf(buf))){ ans ++; //System.out.println(">>> " + i); //System.out.println(buf); break; } } } System.out.println(ans); sc.close(); } }