Pages

Tuesday, September 8, 2009

Euler Problem 22 solution

Time (s): ~0.096
package margusmartseppcode.From_20_to_29;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Problem_22 {
 public static void main(String[] args) throws FileNotFoundException {
  String raw = new Scanner(new File("names.txt")).next();
  String str[] = raw.substring(1, raw.length() - 1).split("\",\"");
  long ls = 0, ts = 0;
  Arrays.sort(str);

  for (int i = 0; i < str.length; i++, ls = 0) {
   for (char c : str[i].toCharArray())
    ls += c - '@';

   ts += ls * (i + 1);
  }
  System.out.println(ts);
 }
}

No comments:

Post a Comment