项目Euler#22 in Java(off 7 364 934)

时间:2012-07-13 02:40:31

标签: java regex string

使用names.txt(右键单击和“保存链接/目标为...”),一个包含超过五千个名字的46K文本文件,首先按字母顺序排序。然后计算每个名称的字母值,将该值乘以列表中的字母位置以获得名称分数。

例如,当列表按字母顺序排序时,值为3 + 15 + 12 + 9 + 14 = 53的COLIN是列表中的第938个名称。因此,COLIN将获得938 53 = 49714的分数。

文件中所有名称分数的总和是多少?

我得到的是863833348,但正确答案是871198282.我不确定是什么问题,我已经尝试了所有的事情,但无法弄清楚问题。

代码:

public static void main(String args[]) throws IOException {

    String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Pattern testRegEx=Pattern.compile("[^\\\"]\\w+[^\\\",]");

    String line;
    String filePath="//Users//painkillerfff//Documents//names.txt";
    BufferedReader readTextFile=new BufferedReader(new FileReader(filePath));

    List<String> nameList=new ArrayList<String>();
    while((line=readTextFile.readLine())!=null){
        //System.out.println(line);
        Matcher matcher=testRegEx.matcher(line);
        while(matcher.find()){
            nameList.add(matcher.group());
            //System.out.println(matcher.group());
        }
    }
    Collections.sort(nameList);

    long sumOfWords=0;
    long namePoint=0;
    long nameRank=1;
    for(String name: nameList){
        namePoint=0;
        for(int i=0;i<name.length();i++){
            namePoint+=alphabet.indexOf(name.charAt(i))+1;
        }
        namePoint=namePoint*nameRank;
        sumOfWords+=namePoint;
        nameRank++;
    }
    System.out.println("The Total Points is: "+sumOfWords);
}

2 个答案:

答案 0 :(得分:3)

你的正则表达式存在缺陷。逗号计数显示有5163个名字,但是你发布的是5130个。

帮助我了解正则表达式,我们可能会想出来。

这是我的版本 - 不需要正则表达式。

    while ((line = readTextFile.readLine()) != null) {
        sb.append(line);
    }
    line = sb.toString();
    line = line.replace("\"", "");
    String[] a = line.split(",");
    System.out.println(a.length);

当然你应该使用真实的变量名,而不是a; - )

编辑 - 我将你的正则表达式改为

    Pattern testRegEx = Pattern.compile("\\w+");

它返回了5163个名字。

答案 1 :(得分:0)

这是另一个实现:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

public class P22 {
    public static void main(String args[]) {
        String content = null;
        try {
            content = new String(Files.readAllBytes(Paths.get("p022_names.txt")));
        } catch (IOException ioe) {
            ioe.getStackTrace();
        }
        String words[] = content.split("\",\"");
        int total = words.length;
        words[0] = words[0].substring(1);
        words[total-1] = words[total-1].substring(0,words[total-1].length()-1);
        Arrays.sort(words);
        int sumOfProducts = 0;
        int base = 'A'-1;
        for ( int i = 0 ; i < total ; i++ ) {
            int sum = 0;
            int len = words[i].length();
            for ( int j = 0 ; j < len ; j++ ) {
                sum+=words[i].charAt(j)-base;
            }
            sumOfProducts+=sum*(i+1);
        }
        System.out.println(sumOfProducts);
    }
}

总时间:56毫秒

<强>解释

  • 读取文件并将其存储到字符串
  • 字符串由","分割。结果是必须针对第一个和最后一个单词调整的单词数组,因为在开头/结尾有一个额外的"
  • 使用Arrays.sort
  • 对数组进行排序
  • 对于每个单词,位置为i+1(因为i0开始),并且使用char实际上是整数的事实来计算总和。
相关问题