Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我用了穷举法 和中国剩余定理 都是超时 郁闷啊穷举法 : import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = ""; List<Integer> list = new LinkedList<Integer>();// 储存的结果 Scanner s = null; try { int p = 0, e = 0, i = 0, d = 0;// 初始值 int p1 = 1, e1 = 1, i1 = 1;// 真正的初始值 int j = 1;// 循环的次数 while (true) { str = in.readLine();// 从键盘读取一行 s = new Scanner(str); // 对该行进行分割 p = s.nextInt() % 23; e = s.nextInt() % 28; i = s.nextInt() % 33;// 获得初始值 d = s.nextInt(); if (p == -1 && e == -1 && i == -1 && d == -1) { in.close(); break; } j = 1; while (true) { p1 = (j - p) % 23; e1 = (j - e) % 28; i1 = (j - i) % 33; if (p1 == 0 && e1 == 0 && i1 == 0 ) { if((j-d)>0) list.add(new Integer(j-d)); else list.add(new Integer(j-d+21252)); break; } j++; } } } catch (IOException v) { v.printStackTrace(); } if (list != null) for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).toString()); } } } 中国剩余定理: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main{ public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = ""; List<Integer> list = new LinkedList<Integer>();// 储存的结果 Scanner s = null; try { int p = 0, e = 0, i = 0, d = 0;// 初始值 int res = 0;//结果 while (true) { str = in.readLine();// 从键盘读取一行 s = new Scanner(str); // 对该行进行分割 p = s.nextInt() % 23; e = s.nextInt() % 28; i = s.nextInt() % 33;// 获得初始值 d = s.nextInt(); s.close(); if (p == -1 && e == -1 && i == -1 && d == -1) { in.close(); break; } res = (5544*p+14421*e+1288*i-d+21252)%21252; if(res!=0) list.add(new Integer(res)); else list.add(new Integer(res+21252)); } } catch (IOException v) { v.printStackTrace(); } if (list!= null) for (int i = 0; i<list.size();i++) { System.out.println("Case " + (i + 1) + ": the next triple peak occurs in " + list.get(i).toString() + " days."); } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator