标签:
将英文数字转换成阿拉伯数字
随机看了几道算法题,以下答案也是别人写的,非本人所写
思路:
三个进行一个划分
代码:
public class POJ_2121 { public static void main(String[] args) { /** * negative, zero, one, two, three, four, five, six, seven, * eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, * sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, * sixty, seventy, eighty, ninety, hundred, thousand, million */ Map map = new HashMap(); map.put("negative", -1); map.put("zero", 0); map.put("one", 1); map.put("two", 2); map.put("three", 3); map.put("four", 4); map.put("five", 5); map.put("six", 6); map.put("seven", 7); map.put("eight", 8); map.put("nine", 9); map.put("ten", 10); map.put("eleven", 11); map.put("twelve", 12); map.put("thirteen", 13); map.put("fourteen", 14); map.put("fifteen", 15); map.put("sixteen", 16); map.put("seventeen", 17); map.put("eighteen", 18); map.put("nineteen", 19); map.put("twenty", 20); map.put("thirty", 30); map.put("forty", 40); map.put("fifty", 50); map.put("sixty", 60); map.put("seventy", 70); map.put("eighty", 80); map.put("ninety", 90); map.put("hundred", 100); map.put("thousand", 1000); map.put("million", 1000000); Scanner scanner = new Scanner(System.in); while(scanner.hasNextLine()){ int sum = 0;//总数 int acc = 0;//累加到某一步的数 int flag = 1; String str1 = scanner.nextLine(); str1 = str1.trim(); if(str1.length()==0){ break; } String[] strs = str1.split(" "); for(String str : strs){ if(str.equals("hundred")){ acc *= (Integer)map.get(str); }else if(str.equals("negative")){ flag = -1; }else if(str.equals("thousand") || str.equals("million")){ sum += acc*((Integer)map.get(str)); acc=0; }else { acc += (Integer)map.get(str); } } System.out.println(flag*(sum+acc)); } } }
标签:
原文地址:http://www.cnblogs.com/sunshisonghit/p/4378036.html