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. lncRNA芯片重注释

    .caret,.dropup>.btn>.caret{border-top-color:#000!important}.label{border:1px solid #000}.table ...

  2. USB-Blaster驱动安装失败——文件哈希值不在指定目录中

    右击此电脑,选择管理,选择设备管理器,更新USB-Blaster驱动出现问题 问题: 文件的哈希值不在指定的目录文件中,如图: 解决办法: Windows键+R→shutdown.exe /r /o ...

  3. Python之文件及文件系统

    open() 方法: Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open( ...

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

    20155222 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 简单JAVA socket * 1 搭建服务器端 * 1 创建ServerSocket对象 ...

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

    20155230 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程:就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发 ...

  6. C语言 结构体学习

    结构体的学习 struct 结构是由基本数据类型构成的.并用一个标识符来命名的各种变量的组合. 结构中可以使用不同的数据类型. 结构说明和结构变量定义 在Turbo C中, 结构也是一种数据类型,可以 ...

  7. HTTP简单教程

    目录 HTTP简介 HTTP工作原理 HTTP消息结构 客户端请求消息 服务器响应消息 实例 HTTP请求方法 HTTP响应头信息 HTTP状态码 HTTP状态码分类 HTTP状态码列表 HTTP c ...

  8. jenkens其实是代码上传工具

    Jenkins 持续集成使用教程 用 jenkins 有什么好处 通过规范化来完成,简单,繁琐,浪费时间的重复工作 规范化工作,以免出现低级错误 实现随时随地任何人一键构建 ...... 安装 jen ...

  9. Mybatis利用拦截器做统一分页

    mybatis利用拦截器做统一分页 查询传递Page参数,或者传递继承Page的对象参数.拦截器查询记录之后,通过改造查询sql获取总记录数.赋值Page对象,返回. 示例项目:https://git ...

  10. Oracle同义词和序列

    同义词:是表.索引.视图的模式对象的一个别名,通过模式对象创建同意词,可以隐藏对象的实际名称和 所有者信息,为对象提供一定的安全性,开发应用程序时:应该尽量避免直接使用表,视图 或其他对象,改用对象的 ...