第一题比较简单,十几分钟:
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String[] s; int n; double sum; while (scanner.hasNextLine()){ s = scanner.nextLine().trim().split(" "); n=Integer.valueOf(s[0]); sum=0; for (int i = 1; i <= 2 * n; i++) { if ((i-1)%2==0){ sum+=1.0/(5*i); }else { sum-=1.0/(5*i); } } System.out.println(String.format("%.4f",sum)); } } }第二题做了一个多小时,一直卡36%,后输出用例查,发现无法判断个位数是不是有效解,遂问考官,给出用例:N=1,M=10,0得到确认,个位数不是有效解。
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String[] s; int N,M; int count; while (scanner.hasNextLine()){ s = scanner.nextLine().trim().split(" "); N=Integer.valueOf(s[0]); M=Integer.valueOf(s[1]); count=0; if (N<10)N=10; if (M<10)M=10; for (int i = N; i <=M; i++) { if (hasHWandPrim(i)){count++; System.out.print(i+" ");} } System.out.println(count); } } public static boolean hasHWandPrim(int num){ Integer temp=num; int temp2; StringBuilder stringBuilder; char[] chars = temp.toString().toCharArray(); for (int i = 0; i < chars.length; i++) { stringBuilder=new StringBuilder(); for (int j = 0; j < chars.length; j++) { if (j!=i)stringBuilder.append(chars[j]); } temp2=Integer.valueOf(stringBuilder.toString()); if (isHW(temp2)&&isPrim(temp2))return true; } return false; } public static boolean isHW(int num){ if (num<0||(num%10==0&& num!=0))return false; int RVNum=0; while (num>RVNum){ RVNum=RVNum*10+num%10; num/=10; } return num==RVNum||num==RVNum/10; } public static boolean isPrim(int num){ if (num<=1)return false; for (int i = 2; i <num/2+1; i++) if (num%i==0)return false; return true; } }暴力AC了
最后3分钟调试出来了,满额头的汗😅
供大家参考。
全部评论
(7) 回帖