jueves, 9 de febrero de 2012

Project Euler 17

public class problem17 {
 static int co = 0;

 public static void main(String[] args) {
  String u[] = { "", "one", "two", "three", "four", "five", "six",
    "seven", "eight", "nine", "ten", "eleven", "twelve",
    "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
    "eighteen", "nineteen", "twenty" };

  String d[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty",
    "seventy", "eighty", "ninety" };

  for (int i = 1; i <= 1000; i++) {
   String num = Integer.toString(i);
   if (num.length() == 1) {
    co = co + u[i].length();
   }
   if (num.length() == 2) {
    if (i < 20) {
     co = co + u[i].length();
    } else {
     co = co + d[num.charAt(0) - 48].length();
     co = co + u[num.charAt(1) - 48].length();
    }
   }
   if (num.length() == 3) {
    String aux = "hundredand";
    co = co + (u[num.charAt(0) - 48].length() + aux.length());
    if (Integer.parseInt(num.substring(1, 3)) < 20) {
     co = co + u[Integer.parseInt(num.substring(1, 3))].length();
    } else {
     co = co + d[num.charAt(1) - 48].length();
     co = co + u[num.charAt(2) - 48].length();
    }
   }
   if (num.length() == 4) {
    co = co + "onethousand".length();
   }
  }
  System.out.println(co - 27);
 }
}

No hay comentarios:

Publicar un comentario