项目Euler#17 in Java(off by 7)

时间:2017-03-11 15:55:25

标签: java

所以我在大约20分钟内写出了这个程序,花了最后40个时间让我的大脑为什么它返回21131而不是21124,这似乎是正确的答案。我知道代码(最有可能)没有问题,因为它适用于我测试过的每一个数字,这可能是我之前应该提出的一些愚蠢...这里是代码:

public class Euler17 {
  public static final String[] numbers = new String[]{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
  public static final String[] tens = new String[]{"", "Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
  public static final String[] teens = new String[]{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
  public static void main (String [] args) {
    int sum = 0;
    for (int a = 0; a <= 1000; a++){
      sum+= toWords(a).replace(" ", "").length();
    }
    System.out.println(sum);
  }
  public static String toWords(int a){
    String w = "";
    // handle the 1000s ;)
    if (a / 1000 > 0){
      w+= numbers[((a % 10000) / 1000)] + " thousand ";
    }
    // handle the 100s
    if (a / 100 > 0){
      w+= numbers[((a % 1000) / 100)] + " hundred";
      if (a % 100 == 0){
        return w;
      }
      w+= " and ";
    }
    // handle the teens
    if ((a / 10) % 10 == 1){
      return w + teens[a % 10];
    }
    // handle the tens
    if (a / 10 > 0){
      w+= tens[((a % 100)/ 10)];
    }
    // handle the ones
    return w + numbers[a % 10];
  }
}

1 个答案:

答案 0 :(得分:1)

我知道我的答案有点晚,但我希望它可以帮助你。

我刚刚看到你的问题,并对自己说了一下吧。无论如何,我尝试解决你遇到的问题,然而,由于variable namings和其他问题,我无法跟上。

我在下面实现的解决方案使用HashMap来解决手头的问题。我已尽力至少在代码中提供评论,因此如果您需要任何进一步的解释,请随时提供评论。

课堂内的全局变量:

private static Map<Integer,String> wordsCollection = new HashMap<>();
private static String andWord = "and", oneThousand = "onethousand";

解决问题的方法:

private static void PE_Problem_17(){
        wordsCollection.put(1,"one"); wordsCollection.put(2,"two"); wordsCollection.put(3,"three");
        wordsCollection.put(4,"four"); wordsCollection.put(5,"five"); wordsCollection.put(6,"six");
        wordsCollection.put(7,"seven"); wordsCollection.put(8,"eight"); wordsCollection.put(9,"nine");
        wordsCollection.put(10,"ten"); wordsCollection.put(11,"eleven"); wordsCollection.put(12,"twelve");
        wordsCollection.put(13,"thirteen"); wordsCollection.put(14,"fourteen"); wordsCollection.put(15,"fifteen");
        wordsCollection.put(16,"sixteen"); wordsCollection.put(17,"seventeen"); wordsCollection.put(18,"eighteen");
        wordsCollection.put(19,"nineteen"); wordsCollection.put(20,"twenty"); wordsCollection.put(30,"thirty");
        wordsCollection.put(40,"forty"); wordsCollection.put(50,"fifty"); wordsCollection.put(60,"sixty");
        wordsCollection.put(70,"seventy"); wordsCollection.put(80,"eighty"); wordsCollection.put(90,"ninety");
        wordsCollection.put(100,"hundred");
        int countLetters = oneThousand.length();

        for (int number = 1; number <= 999; number++){
             if(number <= 20) {
                 countLetters += wordsCollection.get(number).length();  // handle from 1 to 20
                 continue;
             }

             if(number <= 99) {  // handle two digit numbers
                 countLetters += examineTwoDigits(number);
                 continue;
             }

             if(number <= 999){  // handle three digit numbers
                 countLetters += examineThreeDigits(number);
             }
        }

        System.out.println("Result = " + countLetters);  // print the result
}

两位数字的辅助方法:

private static int examineTwoDigits(int number){  // helper method for two digit numbers
         int tempCount = 0;

         if(number == 0) return 0;

         if(number >= 1 && number <= 9){
             tempCount += wordsCollection.get(number).length(); // get length of number from hashMap
             return tempCount;
         }

         if(number % 10 == 0){
             tempCount += wordsCollection.get(number).length(); // get length of number from hashMap
             return tempCount;
         }

         for(int i = 1; i <= 20; i++){
             if(i == number) {
                 tempCount += wordsCollection.get(number).length(); // get length of number from hashMap
                 return tempCount;
             }
         }

        int leftNumber = number / 10;
        String leftNumberString = Integer.toString(leftNumber) + "0";
        int rightNumber = number % 10;
        tempCount += wordsCollection.get(Integer.parseInt(leftNumberString)).length();
        tempCount += wordsCollection.get(rightNumber).length();
        return tempCount; // return the length of words count
}

三位数的辅助方法:

private static int examineThreeDigits(int number){ // helper method for 3 digits
        int tempCount = 0;
        int leftMostDigit = number / 100;  // get leftMost digit
        tempCount += wordsCollection.get(leftMostDigit).length() + wordsCollection.get(100).length();
        StringBuilder builder = new StringBuilder(Integer.toString(number));
        builder.deleteCharAt(0);
        if(Integer.parseInt(builder.substring(0,1)) != 0 || Integer.parseInt(builder.substring(1,2)) != 0){
            tempCount+= andWord.length();
        }
        tempCount += examineTwoDigits(Integer.parseInt(builder.toString()));
        return tempCount;
}