3

我正在Project Euler研究第17题。使用下面的代码,我得到 21088 作为错误的答案。我无法弄清楚问题出在哪里。有人知道我要去哪里错了吗?谢谢。

public class Problem17 {

    public static void main(String[] args) {
        System.out.println(countLetters(1000));
    }

    public static long countLetters(int n) {
        long sum = 0;

        for (int i=1; i<=n; i++){
            sum += getLetters(i);
        }

        return sum;
    }

    public static int getLetters(int n) {
        int letters = 0;

        switch (n) {
            case 0: letters = 0; break;
            case 1: letters = "One".length(); break;
            case 2: letters = "Two".length(); break;
            case 3: letters = "Three".length(); break;
            case 4: letters = "Four".length(); break;
            case 5: letters = "Five".length(); break;
            case 6: letters = "Six".length(); break;
            case 7: letters = "Seven".length(); break;
            case 8: letters = "Eight".length(); break;
            case 9: letters = "Nine".length(); break;
            case 10: letters = "Ten".length(); break;
            case 11: letters = "Eleven".length(); break;
            case 12: letters = "Twelve".length(); break;
            case 13: letters = "Thirteen".length(); break;
            case 14: letters = "Fourteen".length(); break;
            case 15: letters = "Fifteen".length(); break;
            case 16: letters = "Sixteen".length(); break;
            case 17: letters = "Seventeen".length(); break;
            case 18: letters = "Eighteen".length(); break;
            case 19: letters = "Nineteen".length(); break;
            case 20: letters = "Twenty".length(); break;
            case 30: letters = "Thirty".length(); break;
            case 40: letters = "Forty".length(); break;
            case 50: letters = "Fifty".length(); break;
            case 60: letters = "Sixty".length(); break;
            case 70: letters = "Seventy".length(); break;
            case 80: letters = "Eighty".length(); break;
            case 90: letters = "Ninety".length(); break;
            case 100: letters = "OneHundred".length(); break;
            case 200: letters = "TwoHundred".length(); break;
            case 300: letters = "ThreeHundred".length(); break;
            case 400: letters = "FourHundred".length(); break;
            case 500: letters = "FiveHundred".length(); break;
            case 600: letters = "SixHundred".length(); break;
            case 700: letters = "SevenHundred".length(); break;
            case 800: letters = "EightHundred".length(); break;
            case 900: letters = "NineHundred".length(); break;
            case 1000: letters = "OneThousand".length(); break;
        }

        if (letters == 0){
            int length = (int)(Math.log10(n)+1); //how many digits does the number have? 
            int splitNumber;
            for (int i=1;i<=length;i++){
                if (i == 3) letters += 3; //add "and" to the total letters if necessary
                splitNumber = (int) (n % Math.pow(10, i)); 
                letters += getLetters(splitNumber);
                n -= splitNumber;
            }
        }

        return letters;
    }
}

编辑:现在刚刚注意到它正在计算例如 111 为“一百零一”D'OH!直到今晚晚些时候才有时间调整这个,然后会更新。

4

1 回答 1

6

您的问题是,如果您没有从案例列表中获得匹配项,那么您总是将数字分成不同的数字,从较低的数字开始。所以,对于 114,你拿 4 个,剩下 110 个,然后你拿 10 个,剩下 100 个。如果你反过来做,那么对于 114,你移除最高部分(100),那么你剩下 14 个可以与你的开关一起使用。

作为旁注,这个问题非常对称,可能只需要几分钟就可以完成笔和纸。

于 2012-09-14T08:04:52.613 回答