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) {
		}
	}
}
 

你可能感兴趣的:(over)