Java中的Euler 17项目(关闭7个)
所以我在大约20分钟内写出了这个程序,并花了最后40分钟绞尽脑汁想为什么它返回21131而不是21124,这似乎是正确的答案。我知道代码很可能没有什么问题,因为它适用于我测试的每一个数字,这可能是我不久前应该想到的一些愚蠢的东西。。代码如下:Java中的Euler 17项目(关闭7个),java,Java,所以我在大约20分钟内写出了这个程序,并花了最后40分钟绞尽脑汁想为什么它返回21131而不是21124,这似乎是正确的答案。我知道代码很可能没有什么问题,因为它适用于我测试的每一个数字,这可能是我不久前应该想到的一些愚蠢的东西。。代码如下: public class Euler17 { public static final String[] numbers = new String[]{"", "One", "Two", "Three", "Four", "Five", "Six", "
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];
}
}
我知道我的回答有点晚,但我希望它能帮助你 我刚刚看到你的问题,对自己说,让我们试一试。无论如何,我试着解决你遇到的问题,但是,由于变量命名和其他问题,我跟不上 我在下面实现的解决方案使用HashMap来解决手头的问题。我已经尽了最大努力,至少在代码中提供注释,因此如果您需要任何进一步的解释,请随时提供注释 类中的全局变量:
你调试过了吗?那告诉你什么?你有没有试过打印字符串?System.out.printlntoWorda;-最后,它一定是一个错误,只发生一次,并将七个字母添加到最终结果中!是的,我有,它不会打印任何不寻常的东西。也许我根本数不清DHint:查看System.out.printlntoWord1000;的输出;?输出应该是什么?天哪,我真不敢相信我错过了!!!非常感谢。
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;
}