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. java基础之while循环练习(2)

    实现猜数游戏,如果没有猜对随机数,则程序继续,猜对后停止程序. 方法思路: 1:要产生一个随机数,所以需要创建一个随机数对象 Random random=new Random(): 2: 调用随机数对 ...

  2. 20155226 实验一《Java开发环境的熟悉》实验报告

    20155226 实验一<Java开发环境的熟悉>实验报告 一. 实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发 输入cd Code进入Code文件夹里 ...

  3. # 20155236 2016-2017-2 《Java程序设计》第二周学习总结

    20155236 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 对于类型.变量.运算符.流程控制等等的学习.在其中包含着基本的语法元素,还有基本的逻辑语句. ...

  4. tomcat如何禁止显示目录和文件列表

    打开   tomcat的安装目录/conf/web.xml   文件 找到: <servlet> <servlet-name>default</servlet-name& ...

  5. 成都Uber优步司机奖励政策(4月12日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. Yii2.0 技巧总结

    View部分 1. 使用ActiveField中的hint生成提示文字 <?= $form->field($model, 'freightAddedFee')->textInput( ...

  7. redis sentinel介绍

    目录 配置redis主从复制 使用ping命令检查是否启动 主节点查看链接信息 开始部署sentinel 节点 部署sentinel 启动sentinel 演示下故障转移 查看当前sentinel监控 ...

  8. Charles连接苹果及JSON乱码情况解决

    1.  Charles的JSON乱码情况解决: 点击Charles界面上的help—SSL proxying—install Charles Root Certificate,将证书安装到[受信任的根 ...

  9. JavaScript 中函数的定义和调用

    3种函数定义方式: 1.使用关键字 function 来声明并定义函数 function myFunction(a, b) { return a * b; } 调用函数: var x = myFunc ...

  10. https双向认证网站搭建

    新建网站 在搭建网站证书之前,我们先搭建好我们的网站 1.网站基本搭建 为我们的项目新建一个网站,按照如下的步骤来 1,打开IIS,右键单击网站弹出菜单,选择网站(如图1.1.1) 图1.1.1 2, ...