Convert English numerals to Arabic numerals
Randomly read a few algorithm problems, the following answer is written by others, not I write
Ideas:
Three to make a distinction between
Code:
Public classpoj_2121 { Public Static voidMain (string[] args) {/*** Negative, zero, one, three, four, five, six, seven, * Eight, nine, ten, eleven, twelve, thi Rteen, fourteen, fifteen, * Sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, Fifty, * s Ixty, Seventy, eighty, ninety, Hundred, Thousand, million*/map Map=NewHashMap (); Map.put ("Negative",-1); Map.put ("Zero", 0); Map.put ("One", 1); Map.put ("Both", 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=NewScanner (system.in); while(Scanner.hasnextline ()) {intsum = 0;//Total intACC = 0;//the number that accumulates to a step intFlag = 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)); } } }
View Code
Algorithmic questions-converting English numerals to Arabic numerals