0

它试图解决 Code Jam 2014 中的第二个任务。不幸的是,我有 InputMisMatchException?我认为问题是第一个数字不是双倍的。我应该如何阅读这些信息?链接到任务。

Input:    
    4
    30.0 1.0 2.0
    30.0 2.0 100.0
    30.50000 3.14159 1999.19990
    500.0 4.0 2000.0

我的Java代码:

public class CookieClickerAlpha {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);

        int n = (int) sc.nextDouble();

        double priceForFarm, plusCookies, goal;
        double currentTime = 0.0, timeWithFarm, 
                timeWithoutFarm, timeForFarm, resultTime;
        double currCookies = 0.0, cookiesPerSec = 2.0;

        for (int i = 0; i < n; i++) {

            priceForFarm = sc.nextDouble();
            plusCookies = sc.nextDouble();
            goal = sc.nextDouble();

            while (currCookies < goal) {

                timeForFarm = (priceForFarm - currCookies) / cookiesPerSec;

                timeWithFarm = timeForFarm + 
                        goal / (cookiesPerSec + plusCookies);
                timeWithoutFarm = (goal - currCookies) / cookiesPerSec;

                if (timeWithFarm < timeWithoutFarm) {
                    currCookies = 0.0;
                    cookiesPerSec += plusCookies;
                    currentTime += timeForFarm;
                }
                else {
                    currentTime += goal / cookiesPerSec;
                    currCookies = goal;
                }

            }

            pw.print("Case #" + (i + 1) + ": ");
            pw.println(currentTime);

        }

        sc.close();
        pw.close();
    }
}
4

1 回答 1

0

使用sc.hasNextDouble()/hasNextInt()检查 nxt 编号是否为double/ int,然后nextInt(), nextDouble()根据 hasNextXXX 返回的内容使用。

编辑 :

要首先读取整数,请执行以下操作:

if(sc.hasNextInt()){
n=sc.nextInt();
}
于 2014-06-12T10:52:52.987 回答