package margusmartseppcode.From_40_to_49; import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Scanner; import java.util.Map; public class Problem_42 { static int sumStringChars(String s) { if (s == null) return 0; int n = s.length(), sum = 0; for (int i = 0; i < n; i++) sum += s.charAt(i) - '@'; return sum; } public static void main(String[] args) throws FileNotFoundException { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); String raw = new Scanner(new File("words.txt")).next(); String str[] = raw.substring(1, raw.length() - 1).split("\",\""); int count = 0; for (String s : str) { int tmp = sumStringChars(s); map.put(tmp, map.containsKey(tmp) ? map.get(tmp) + 1 : 1); } for (Integer i : map.keySet()) if ((((Math.sqrt(1 + 8 * i) - 1) / 2) % 1 == 0)) count += map.get(i); System.out.println(count); } }
Wednesday, September 9, 2009
Euler Problem 42 solution
Time (s): ~0.067
Labels:
Euler Problem 40-49,
File,
HashMap,
Scanner,
Strings,
triangle words
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment