`
mycream
  • 浏览: 54215 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

POJ 1003 Hangover

阅读更多

一开始没看清题目,不明白要做什么。在转了圈,明白了就是求 1/2 + 1/3 + ... + 1/x >= n 的最小X。

代码如下:

import java.util.Scanner;

public class Main {
	public int checkCards(double c) {
		double sum = 0.0;
		int i = 1;
		for (; i < Integer.MAX_VALUE; i++) {
			sum += 1.0d / (i + 1);
			if (sum > c) {
				break;
			}
		}
		return i;
	}

	public void run() throws Exception {
		Scanner scan = new Scanner(System.in);
		String input = scan.nextLine();
		while (!input.equals("0.00")) {
			double c = Double.parseDouble(input);
			int cards = checkCards(c);
			System.out.println(cards + " card(s)");

			input = scan.nextLine();
		} 
	}

	public static void main(String[] args) {
		Main m = new Main();
		try {
			m.run();
		} catch (Exception e) {
		}
	}
}
 
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics