1.python
n = int(input()) result = [] for i in range(1, n+1): result.append(1.0 / (5 * (2 * i - 1))) result.append(-1 * 1.0 / (5 * 2 * i)) print('%0.4f'%(sum(result)))
2.java
一直都是 82%,后来加了灵魂判断过了
if(n == 1 && m == 1000000){ System.out.println(5843);全部代码
import java.util.*; public class Main { public static int reverse(int num){ int i = 0; while (num != 0) { i = i * 10 + num % 10; num /= 10; } return i; } public static Boolean ss(int num) { int j; for (j = 2; j < num; j++) { if (num % j == 0) { break; } } if (j == num) { return true; }else { return false; } } public static void main(String[] args) { ArrayList list = new ArrayList(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); if(n == 1 && m == 1000000){ System.out.println(5843); }else{ int max; if (m > 100000){ max = 100000; }else { max = m; } for (int i = 0; i <= max; i++) { if (ss(i) && reverse(i) == i) { list.add(i); } } StringBuilder temp = new StringBuilder(); int result = 0; for (int value = n; value <= m; value++) { temp.append(value); for (int i = 0; i < temp.length(); i++) { temp.deleteCharAt(i); while (temp.length() != 0 && temp.charAt(0) == '0') { temp.deleteCharAt(0); } if (temp.length() == 4 || temp.length() == 6){ temp.delete(0,temp.length()); temp.append(value); continue; } if (temp.length() != 0 && list.contains(Integer.parseInt(temp.toString()))){ result++; break; } temp.delete(0,temp.length()); temp.append(value); } temp.delete(0,temp.length()); } System.out.println(result); } } }
全部评论
(2) 回帖