Project Euler 22的麻烦

时间:2017-05-19 20:00:02

标签: java debugging

所以,我正在玩Project Euler并且我的代码遇到了问题。这还不完整,我还没想冒险实际阅读文件。现在我只是使用一个包含一些单词的ArrayList。这是我的代码:

public static void euler22test(){ //flaw somewhere in this
    ArrayList<String> list = new ArrayList<>(read()); //gets information from file
    int sum = 0;
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //for calculating namescore
    for (int i = 0; i < list.size(); i++){ //for item in list
        String word = list.get(i); //gets string at index i in array
        for (int k = 0; k < word.length(); k++){ //for each letter in word
            int wordSum = 0; //the sum for current word
            char c = word.charAt(k); //gets letter for each index of word
            for (int a = 0; a < alphabet.length(); a++){ //checks what letter it is
                if (c == alphabet.charAt(a)){ //if they are the same
                    wordSum += alphabet.indexOf(c) + 1; //gets place in alphabet
                    sum += wordSum * i; //adds wordSum * place in list to sum.
                }
            }
        }
    }
    System.out.println("Actual: 1720"); //what it should print out
    System.out.println("Calculated: "+sum); //prints 1295
}
public static ArrayList<String> read(){//reads from file
    //currently just returns the below list
    //will eventually read from a file
    ArrayList<String> list = new ArrayList<>();
    list.add("MARY");//5
    list.add("PATRICIA");//6
    list.add("LINDA");//4
    list.add("BARBARA"); //1
    list.add("ELIZABETH");//2
    list.add("JENNIFER");//3
    list.add("MARIA");//4
    return list;
}

正如评论所说,跑步时,这打印出1290.我亲手计算出真实值是1720.这是我的数学:

BARBARA
wordSum: 1(2+1+18+2+1+18+1)
sum: 43
ELIZABETH
wordSum: 2(5+12+9+26+1+2+5+20+8)
sum: 219
JENNIFER
wordSum: 3(10+5+14+14+9+6+5+18)
sum: 462
LINDA
wordSum: 4(12+9+14+4+1)
sum: 622
MARIA
wordSum: 5(13+1+18+9+1)
sum: 832
MARY
wordSum: 6(13+1+18+25)
sum: 1174
PATRICIA
wordSum: 7(17+1+20+18+9+3+9+1)
sum: 1720

我无法在代码中找到错误。

1 个答案:

答案 0 :(得分:0)

您的代码有些错误。首先,在计算每个字符的值之后,而不是在计算名称的整个wordSum值之后,将wordSum添加到Sum。 wordSum值也应该乘以i + 1而不是i,因为i从0开始。这是固定代码。

public static void main(String[] args) { //flaw somewhere in this
    ArrayList<String> list = new ArrayList<>(read()); //gets information from file
    int sum = 0;
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //for calculating namescore
    for (int i = 0; i < list.size(); i++) { //for item in list
        String word = list.get(i); //gets string at index i in array
        int wordSum = 0; //the sum for current word
        for (int k = 0; k < word.length(); k++) { //for each letter in word
            char c = word.charAt(k); //gets letter for each index of word
            for (int a = 0; a < alphabet.length(); a++) { //checks what letter it is
                if (c == alphabet.charAt(a)) { //if they are the same
                    wordSum += alphabet.indexOf(c) + 1; //gets place in alphabet
                }
            }
            System.out.println(wordSum);
        }
        sum += wordSum * (i+1); //adds wordSum * place in list to sum.
    }
    System.out.println("Actual: 1713"); //what it should print out
    System.out.println("Calculated: " + sum); //prints 1295
}

此外,似乎在手工计算该值时犯了一个错误。 PATRICIA中的“ P”应为16而不是17,导致总数为1713。