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) 懂了这个就好办了,直接贴代码吧,个人对代码的要求没写那么精炼,确实有待改进:

 import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//String key="nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve";
Scanner sc = new Scanner(System.in);
int T=sc.nextInt();
sc.nextLine();
while(T>0){
String input;
input=sc.nextLine();
input.toLowerCase();
String []word = input.split(" "); long dan[]=new long[word.length+1];
//获取最大的单位
//初始化
for (int i = 0; i < word.length; i++) {
String key = word[i];
switch (key) {
case "hundred":
dan[i]=100;
break;
case "thousand":
dan[i]=1000;
break;
case "million":
dan[i]=1000000;
break;
default:
dan[i]=0;
break;
}//switch
}//for
long result = getResult(word, dan, 0, word.length);
System.out.println(result);
T--;
}
} //first 包含,last不包含
public static long getResult(String word[],long dan[],int first,int last){
long res=0;
long max=1;
int maxnum=0; //找到单位最大的那个
for (int i = first; i < last; i++) {
if(dan[i]>max){
max =dan[i];
maxnum=i;
}
}
long left=0;
long right=0;
int num=0; if(max>1){
//这里递归调用
left=getResult(word,dan,first,maxnum); right= getResult(word, dan, maxnum+1, last); res=left*max+right; }else{ for (int i = first; i < last; i++) {
//
String key= word[i];
switch (key) {
case "zero":
num += 0;
break;
case "one":
num += 1;
break;
case "two":
num += 2;
break;
case "three":
num += 3;
break;
case "four":
num += 4;
break;
case "five":
num += 5;
break;
case "six":
num += 6;
break;
case "seven":
num += 7;
break;
case "eight":
num += 8;
break;
case "nine":
num += 9;
break;
case "ten":
num += 10;
break;
case "eleven":
num += 11;
break;
case "twelve":
num += 12;
break;
case "thirteen":
num += 13;
break;
case "fourteen":
num += 14;
break;
case "fifteen":
num += 15;
break;
case "sixteen":
num += 16;
break;
case "seventeen":
num += 17;
break;
case "eighteen":
num += 18;
break;
case "nineteen":
num += 19;
break;
case "twenty":
num += 20;
break;
case "thirty":
num += 30;
break;
case "forty":
num += 40;
break;
case "fifty":
num += 50;
break;
case "sixty":
num += 60;
break;
case "seventy":
num += 70;
break;
case "eighty":
num += 80;
break;
case "ninety":
num += 90;
break;
case "hundred":
break;
case "thousand":
break;
case "million":
break;
case "and":
break;
default:
break;
}
}//for
res = num;
} return res;
}
}

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

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

ACM解题之在线翻译 Give Me the Number的更多相关文章

  1. Excel里内嵌在线翻译

    本来寻思着继续写点系统运行日志跟踪技术的,但早晨哥家领导从单位打来电话,让帮助她的闺蜜搞一个excel翻译的问题,总部IT搞不定.我过去是用excel做了几年工作,却都是些数学计算,跟翻译也扯不上啊: ...

  2. gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典

    gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典 gravitas

  3. victim是什么意思_victim在线翻译_英语_读音_用法_例句_海词词典

    victim是什么意思_victim在线翻译_英语_读音_用法_例句_海词词典 victim

  4. 【milonga】什么意思_英语milonga在线翻译_有道词典

    [milonga]什么意思_英语milonga在线翻译_有道词典 milonga 网络释义英英释义   米隆加 本届探戈艺术节表演最受观众欢迎的是热情欢快的米隆加(Milonga)舞曲探戈,为了吸引年 ...

  5. delphi 利用HTTP的POST方法做个在线翻译的小工具 good

    最近做了一个英汉小翻译的东东,用的是VC,ADO + Access访问数据库,单词数据库是从金山打字通2002弄来的.后来想了想,想再加个在线翻译的功能,记得经常使用GOOGLE翻译网站的在线翻译,也 ...

  6. brutal是什么意思_brutal在线翻译_英语_读音_用法_例句_海词词典

    brutal是什么意思_brutal在线翻译_英语_读音_用法_例句_海词词典 brutal

  7. birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典

    birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典 birdnest

  8. Python爬虫教程-16-破解js加密实例(有道在线翻译)

    python爬虫教程-16-破解js加密实例(有道在线翻译) 在爬虫爬取网站的时候,经常遇到一些反爬虫技术,比如: 加cookie,身份验证UserAgent 图形验证,还有很难破解的滑动验证 js签 ...

  9. C++调用有道翻译API实现在线翻译之发声篇

    大概半月前写了一篇博文:C++中使用Curl和JsonCpp调用有道翻译API实现在线翻译, 得到大家的热情捧场,有人看了文章说要是能发声不是更好,我觉得说的也是哈,能听到专家的标准发音,那该是多美的 ...

随机推荐

  1. 探秘GO语言《比较C#与GO的性能--XML序列化》

    今天对GO和NET的XML字符串序列化成对象列表做了一个性能比较,得出一些结论. GO的代码: package main import ( "encoding/xml" " ...

  2. 20155209 实验三 敏捷开发与XP实践

    20155209 实验三 敏捷开发与XP实践 实验内容 1. XP基础 2. XP核心实践 3. 相关工具 提交点一: 在IDEA中使用工具(Code->Reformate Code)把下面代码 ...

  3. 20155216 2016-2017-2《Java程序设计》课程总结

    20155216 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:简要内容:我对师生关系的见解 预备作业2:简要内容:有关C语言学习调查以及学习 ...

  4. 20155315 2016-2017-2 《Java程序设计》第十周学习总结

    学习目标 了解计算机网络基础 掌握Java Socket编程 理解混合密码系统 掌握Java 密码技术相关API的使用 学习资源 Java和Android开发学习指南(第二版)(EPUBIT,,Jav ...

  5. 关于homebrew使用时遇到的问题: Error: Could not symlink bin/gdb/usr/local/bin is not writable.

    # 关于homebrew使用时遇到的问题: Error: Could not symlink bin/gdb/usr/local/bin is not writable. 这是我在给我的Mac电脑安装 ...

  6. 初识Linux的感受与对它的印象——20155328张钰清

    之前从未接触过虚拟机的我,由于这次寒假预备作业,稍稍地认识了一下Linux操作系统. 在自己笔记本上安装Linux操作系统 根据老师提供的<基于VirtualBox虚拟机安装Ubuntu图文教程 ...

  7. PostgreSQL的pg_stats学习

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL统计信息索引页     回到顶级页面:PostgreSQL索引页 对于pg_stas,说明文档在这里: http://w ...

  8. [并发并行]_[线程模型]_[Pthread线程使用模型之二 工作组work crew]

    Pthread线程使用模型之二工作组(Work crew) 场景 1.一些耗时的任务,比如分析多个类型的数据, 是独立的任务, 并不像 pipeline那样有序的依赖关系, 这时候pipeline就显 ...

  9. Mysql:存储过程游标不进循环的原因详解

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客给刚接触存储过程的朋友做个引导作用,目的是解决游标不走循环 很多人发现他的游标,无论是嵌套循环还是单层 ...

  10. 如何在makfile中查看变量的值

    在makefile中查看变量的取值是多少应该是一个比较麻烦的问题,但是本大神自己研究出一个十分方便的方法.这个方法十分简单.现在介绍如下 如果在一个十分复杂庞大的makefile文件中,有个地方用到一 ...