今日题目:POJ1005、POJ1006
(一)POJ1005
题目地址:http://poj.org/problem?id=1005
No Problem!
源代码如下:
import java.util.Scanner; //POJ 1005 /*Date: 2016.07.17 * 1st: Accepted */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 1; i <= n; i++){ double x = sc.nextDouble(); double y = sc.nextDouble(); double l = Math.sqrt(x * x + y * y); int j = 1; while(Math.sqrt(2 * 50 * j / Math.PI) < l){ j++; } System.out.println("Property " + i + ": This property will begin eroding in year " + j +"."); } System.out.println("END OF OUTPUT."); } }
(二)POJ1006 题目地址:http://poj.org/problem?id=1006
1.一开始算法错误,如此写:
潜移默化中“以0,0,0为起点,每个循环各经历了p, e, i天”,当然错误。int s = 1; while(true){ if((p + s) % 23 == 0 && (e + s) % 28 == 0 && (i + s) % 33 == 0){ System.out.println("Case " + c + ": the next triple peak occurs in " + (s - d) + " days."); break; } s++; }
2.恐怕逐日累加会超时,结果并未超时。可有改进算法,未超时故未用。
源代码如下:
3.发现网上大牛之解答,使用“中国剩余定理”。可以有效减少时间复杂度,参见: http://www.cnblogs.com/walker01/archive/2010/01/23/1654880.htmlimport java.util.Scanner; //POJ 1006 /*Date: 2016.07.17 * 1st: Accepted */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int c = 0; while(true){ c++; int p, e, i, s; p = sc.nextInt(); e = sc.nextInt(); i = sc.nextInt(); d = sc.nextInt(); if(p < 0){ break; } int s = d + 1; s++; while(true){ if((s - p) % 23 == 0 && (s - e) % 28 == 0 && (s - i) % 33 == 0){ System.out.println("Case " + c + ": the next triple peak occurs in " + (s - d) + " days."); break; } s++; } } } }
算法日记(Java实现)第20160717(3)期——POJ1005/POJ1006
最新推荐文章于 2019-06-03 08:48:40 发布