Algorithmic questions-converting English numerals to Arabic numerals

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.