算法日记(Java实现)第20160717(3)期——POJ1005/POJ1006

今日题目: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.一开始算法错误,如此写:

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++;
}
潜移默化中“以0,0,0为起点,每个循环各经历了p, e, i天”,当然错误。

2.恐怕逐日累加会超时,结果并未超时。可有改进算法,未超时故未用。

源代码如下:

import 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++;
			}
		}
	}
}
3.发现网上大牛之解答,使用“中国剩余定理”。可以有效减少时间复杂度,参见: http://www.cnblogs.com/walker01/archive/2010/01/23/1654880.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值