2017-03-11 15 views
0

私は約20分でプログラムを書いて、最後の40は21124の代わりに21131を返す理由について私の脳を苦労させました。これは正解です。プロジェクトオイラー#17 Javaで(off by 7)

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]; 
    } 
} 
+0

は、あなたがそれをデバッグしていますか?それが何を伝えましたか? – Carcigenicate

+0

文字列を印刷したことがありますか? 'System.out.println(toWord(a));' - 最終的には一度発生し、最終結果に7文字を追加するエラーでなければなりません! –

+0

はい、私はそれは普通のものを印刷しません。たぶん私は結局数えることはできません。:D – user2177595

答えて

1

I:私はここのコードです...それはおそらく私がしばらく前に出ている必要があることを愚かなものだ、それは私がテストしたすべての数のために働くので、それは(ほとんど)コードで何か間違っていないと知っています私の答えが少し遅れていることを知っていますが、それがあなたを助けることを願っています。

私はちょうどあなたの質問を見て、自分自身に言いました。とにかく、あなたが持っていた問題を解決しようとしましたが、variableという名前やその他の問題のために追いつくことができませんでした。

私が以下で実装した解決策は、手元の問題を解決するためにHashMapを使用しています。私は少なくともコード内にコメントを提供するために最善を尽くしたので、それ以上の説明が必要な場合はコメントを自由に記述してください。クラス内

グローバル変数:2桁の番号について

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 Map<Integer,String> wordsCollection = new HashMap<>(); 
private static String andWord = "and", oneThousand = "onethousand"; 

方法

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 
} 

3桁の数字のためのヘルパーメソッド:

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; 
} 
関連する問題