gpt4 book ai didi

java - Java 中的 Project Euler #17(落后 7)

转载 作者:行者123 更新时间:2023-12-02 13:41:13 31 4
gpt4 key购买 nike

所以我用了大约 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];
}
}

最佳答案

我知道我的回答有点晚了,但希望能帮到你。

我刚刚看到你的问题并对自己说让我们尝试一下。无论如何,我尝试解决您遇到的问题,但是,由于变量命名和其他问题,我无法跟上。

我在下面实现的解决方案使用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;
}

关于java - Java 中的 Project Euler #17(落后 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42737279/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com