POJ1003 Hangover

这个我使用暴力法解题

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double[] dArray = createDoubleArray();
        while(true){
            double d = scanner.nextDouble();
            if(d == 0.0) break;
            int idx = findInArray(dArray,d);
            System.out.println(idx+" card(s)");
        }
    }

    private static double[] createDoubleArray(){
        double d = 0.0;
        double[] dArray = new double[300];
        for(int idx=1;idx < 300;idx++){
            d += 1.0/(idx+1);
            dArray[idx]=d;
        }
        return dArray;
    }
    
    private static int findInArray(double[] dArray,double value){
        for (int i = 1; i < dArray.length; i++) {
            double v = dArray[i];
            if(v > value){
                return i;
            }
        }
        return 0;
    }

}
 

你可能感兴趣的:(over)