• ACM解题之在线翻译 Give Me the Number


    Give Me the Number

    Time Limit: 2 Seconds                                     Memory Limit: 65536 KB                            

    Numbers in English are written down in the following way (only numbers less than 109 are considered). Number  abc,def,ghi is written as "[abc] million [def] thousand [ghi]". Here "[xyz] " means the written down number xyz .  

    In the written down number the part "[abc] million" is omitted  if abc = 0 , "[def] thousand" is omitted if  def = 0 , and "[ghi] " is omitted if ghi = 0 .  If the whole number is equal to 0 it is written down as "zero". Note that words "million" and "thousand" are singular even if the number of millions or thousands respectively is greater than one.

    Numbers under one thousand are written down in the following way. The number xyz is written as "[x] hundred and [yz] ”. ( If yz = 0 it should be only “[x] hundred”. Otherwise if y = 0 it should be only “[x] hundred and [z]”.) Here "[x] hundred and" is omitted if x = 0 . Note that "hundred" is also always singular.

    Numbers under 20 are written down  as  "zero",  "one",  "two",  "three",  "four",  "five",  "six", "seven",  "eight",  "nine",  "ten",  "eleven",  "twelve",  "thirteen", "fourteen",  "fifteen",  "sixteen",  "seventeen",  "eighteen", and  "nineteen" respectively.  Numbers from 20 to 99 are written down in the following way. Number xy is written as "[x0] [y] ", and numbers divisible by ten are written as  "twenty",  "thirty",  "forty",  "fifty",  "sixty",  "seventy", "eighty", and  "ninety"  respectively.

    For example, number 987,654,312 is written down as "nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve", number 100,000,037 as "one hundred million thirty seven", number 1,000 as "one thousand". Note that "one" is never omitted for millions, thousands and hundreds.

    Give you the written down words of a number, please give out the original number.

    Input

    Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 1900) which is the number of test cases. It will be followed by T consecutive test cases. 

    Each test case contains only one line consisting of a sequence of English words representing a number.

    Output

    For each line of the English words output the corresponding integer in a single line. You can assume that the integer is smaller than 109.

    Sample Input

    3
    one
    eleven
    one hundred and two
    

    Sample Output

    1
    11
    102

    贴上题目的地址:

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971


    先说题目吧,这玩意就是一个在线翻译的东东,就是把英文表示的数字转换成阿拉伯数字。
    然后再说解题的思路:
    1.数字,比如1,2,3,11,30这种的,肯定没办法通过计算来得到,只能switch case,这些就是最小的基本单位。
    2.然后是 32这种由30 + 2 就可以得出
    3.下来就是hundred这些有单位的(不是说没有十位个位哈,只是十位个位没有单独的字符串来表示),one hundred就是 1*100
    4.上面的数都比较特殊,也是基本单位,然后就是推广了。
      three hundred an twelve 从hundred单位这里分开,整个数的结果就等于 L*单位+R 就是左边的数值乘上单位,然后加上单位右面的数字。
    然后对L,R的值,又可以递归的调用这个方法,得出它的值,直到单位小于100的,就直接把数字相加(本质上就是L*1+R)

    懂了这个就好办了,直接贴代码吧,个人对代码的要求没写那么精炼,确实有待改进:

      1 import java.util.Scanner;
      2 public class Main {
      3     public static void main(String[] args) {
      4         //String key="nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve";
      5         Scanner sc = new Scanner(System.in);
      6         int T=sc.nextInt();
      7         sc.nextLine();
      8         while(T>0){
      9             String input;
     10             input=sc.nextLine();
     11             input.toLowerCase();
     12             String []word = input.split(" ");
     13 
     14             long dan[]=new long[word.length+1];
     15             //获取最大的单位
     16             //初始化
     17             for (int i = 0; i < word.length; i++) {
     18                 String key = word[i];
     19                 switch (key) {
     20                 case "hundred":
     21                     dan[i]=100;
     22                     break;
     23                 case "thousand":
     24                     dan[i]=1000;
     25                     break;
     26                 case "million":
     27                     dan[i]=1000000;
     28                     break;
     29                 default:
     30                     dan[i]=0;
     31                     break;
     32                 }//switch
     33             }//for
     34             long result = getResult(word, dan, 0, word.length);
     35             System.out.println(result);
     36             T--;
     37         }
     38     }
     39 
     40     //first 包含,last不包含
     41     public static long getResult(String word[],long dan[],int first,int last){
     42         long res=0;
     43         long max=1;
     44         int maxnum=0;
     45 
     46         //找到单位最大的那个
     47         for (int i = first; i < last; i++) {
     48             if(dan[i]>max){
     49                 max =dan[i];
     50                 maxnum=i;
     51             }
     52         }
     53         long left=0;
     54         long right=0;
     55         int num=0;
     56 
     57         if(max>1){
     58                                //这里递归调用
     59             left=getResult(word,dan,first,maxnum);
     60 
     61             right= getResult(word, dan, maxnum+1, last);
     62 
     63             res=left*max+right;
     64 
     65         }else{
     66 
     67             for (int i = first; i < last; i++) {
     68                 //
     69                 String key= word[i];
     70                 switch (key) {
     71                 case "zero":
     72                     num += 0;
     73                     break;
     74                 case "one":
     75                     num += 1;
     76                     break;
     77                 case "two":
     78                     num += 2;
     79                     break;
     80                 case "three":
     81                     num += 3;
     82                     break;
     83                 case "four":
     84                     num += 4;
     85                     break;
     86                 case "five":
     87                     num += 5;
     88                     break;
     89                 case "six":
     90                     num += 6;
     91                     break;
     92                 case "seven":
     93                     num += 7;
     94                     break;
     95                 case "eight":
     96                     num += 8;
     97                     break;
     98                 case "nine":
     99                     num += 9;
    100                     break;
    101                 case "ten":
    102                     num += 10;
    103                     break;
    104                 case "eleven":
    105                     num += 11;
    106                     break;
    107                 case "twelve":
    108                     num += 12;
    109                     break;
    110                 case "thirteen":
    111                     num += 13;
    112                     break;
    113                 case "fourteen":
    114                     num += 14;
    115                     break;
    116                 case "fifteen":
    117                     num += 15;
    118                     break;
    119                 case "sixteen":
    120                     num += 16;
    121                     break;
    122                 case "seventeen":
    123                     num += 17;
    124                     break;
    125                 case "eighteen":
    126                     num += 18;
    127                     break;
    128                 case "nineteen":
    129                     num += 19;
    130                     break;
    131                 case "twenty":
    132                     num += 20;
    133                     break;
    134                 case "thirty":
    135                     num += 30;
    136                     break;
    137                 case "forty":
    138                     num += 40;
    139                     break;
    140                 case "fifty":
    141                     num += 50;
    142                     break;
    143                 case "sixty":
    144                     num += 60;
    145                     break;
    146                 case "seventy":
    147                     num += 70;
    148                     break;
    149                 case "eighty":
    150                     num += 80;
    151                     break;
    152                 case "ninety":
    153                     num += 90;
    154                     break;
    155                 case "hundred":
    156                     break;
    157                 case "thousand":
    158                     break;
    159                 case "million":
    160                     break;
    161                 case "and":
    162                     break;
    163                 default:
    164                     break;
    165                 }                
    166             }//for
    167             res = num;
    168         }
    169 
    170         return res;
    171     }
    172 }
    173                 

    测试结果如下,直接通过了。

    明显的能感觉到java程序的效率和c/c++真没法比。

  • 相关阅读:
    android个人中心界面
    开课第十二周周总结
    android----从相册中选择照片
    《程序员修炼之道:从小工到专家》 阅读笔记
    android----pull解析方式
    开课第十一周周总结
    解决数据库连接时区的问题
    实现数组中连续子数组值和最大
    开课第十周周总结
    解 idea(.IntelliJIdea2019.3)双击打不开的原因
  • 原文地址:https://www.cnblogs.com/mediciyan/p/4047998.html
Copyright © 2020-2023  润新知