京东 8.6 笔试 编程题 AK
数列求和
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); double f = 0d; double sum = 0d; for(int i = 1; i <= n; i++){ f = 1d / (5 * (2 * i - 1)) - 1d / (5 * 2 * i); sum += f; } System.out.printf("%.4f", sum); } }
回文素数的个数
import java.util.*; public class Main{ static Map<String, Boolean> map; public static void main(String[] args){ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M = sc.nextInt(); int ans = 0; map = new HashMap(); for(int i = Math.max(10, N); i <= M; i++) { String s = i + ""; for(int j = 0; j < s.length(); j++) { String b = s.substring(0, j) + s.substring(j + 1); if(is(b)) { ans++; break; } } } System.out.print(ans); } public static boolean is(String s) { if(!map.containsKey(s)){ s = Integer.parseInt(s) + ""; if(s.equals(new StringBuilder(s).reverse().toString())) { int n = Integer.parseInt(s); boolean hui = true; if(n == 0 || n == 1) {hui = false;} else { for(int i = 2; i < n; i++) { if(n / i * i == n) { hui = false; break; } } } map.put(s, hui); }else { map.put(s, false); } } return map.get(s); } }