Don't like ads? PRO users don't see any ads ;-)
Guest

zankooo

By: a guest on Jun 26th, 2012  |  syntax: Java  |  size: 1.86 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4. public class uva486 {
  5.         static String words[] = { "zero", "one", "two", "three", "four", "five",
  6.                         "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
  7.                         "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
  8.                         "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty",
  9.                         "sixty", "seventy", "eighty", "ninety" };
  10.         static int value[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  11.                         15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90 };
  12.         static HashMap<String, Integer> map;
  13.  
  14.         public static void main(String[] args) {
  15.                 Scanner sc = new Scanner(System.in);
  16.                 map = new HashMap<String, Integer>();
  17.                 for (int i = 0; i < value.length; i++)
  18.                         map.put(words[i], value[i]);
  19.                 while (sc.hasNext()) {
  20.                         int ans = 1;
  21.                         String hundred = null, thousand = null, million = null;
  22.                         StringBuilder in = new StringBuilder(sc.nextLine());
  23.                         if (in.indexOf("negative") != -1) {
  24.                                 ans = -1;
  25.                                 in = in.delete(0, 9);
  26.                         }
  27.                         int m = in.indexOf("million");
  28.                         if (m != -1) {
  29.                                 million = in.substring(0, m);
  30.                                 in = in.delete(0, m + 8);
  31.                         }
  32.                         int t = in.indexOf("thousand");
  33.                         if (t != -1) {
  34.                                 thousand = in.substring(0, t);
  35.                                 in = in.delete(0, t + 9);
  36.                         }
  37.                         if (in.length() > 2)
  38.                                 hundred = in.toString();
  39.                         ans *= solve(million) * 1000000 + solve(thousand) * 1000
  40.                                         + solve(hundred);
  41.                         System.out.println(ans);
  42.                 }
  43.         }
  44.  
  45.         private static int solve(String s) {
  46.                 if (s == null)
  47.                         return 0;
  48.                 String[] in = s.split(" ");
  49.                 if (in.length == 1)
  50.                         return map.get(in[0]);
  51.                 else if (in.length == 2)
  52.                         if (in[1].equals("hundred"))
  53.                                 return map.get(in[0]) * 100;
  54.                         else
  55.                                 return map.get(in[0]) + map.get(in[1]);
  56.                 else if (in.length == 3)
  57.                         return map.get(in[0]) * 100 + map.get(in[2]);
  58.  
  59.                 return map.get(in[0]) * 100 + map.get(in[2]) + map.get(in[3]);
  60.         }
  61. }