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) 懂了这个就好办了,直接贴代码吧,个人对代码的要求没写那么精炼,确实有待改进:
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的更多相关文章
- Excel里内嵌在线翻译
本来寻思着继续写点系统运行日志跟踪技术的,但早晨哥家领导从单位打来电话,让帮助她的闺蜜搞一个excel翻译的问题,总部IT搞不定.我过去是用excel做了几年工作,却都是些数学计算,跟翻译也扯不上啊: ...
- gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典
gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典 gravitas
- victim是什么意思_victim在线翻译_英语_读音_用法_例句_海词词典
victim是什么意思_victim在线翻译_英语_读音_用法_例句_海词词典 victim
- 【milonga】什么意思_英语milonga在线翻译_有道词典
[milonga]什么意思_英语milonga在线翻译_有道词典 milonga 网络释义英英释义 米隆加 本届探戈艺术节表演最受观众欢迎的是热情欢快的米隆加(Milonga)舞曲探戈,为了吸引年 ...
- delphi 利用HTTP的POST方法做个在线翻译的小工具 good
最近做了一个英汉小翻译的东东,用的是VC,ADO + Access访问数据库,单词数据库是从金山打字通2002弄来的.后来想了想,想再加个在线翻译的功能,记得经常使用GOOGLE翻译网站的在线翻译,也 ...
- brutal是什么意思_brutal在线翻译_英语_读音_用法_例句_海词词典
brutal是什么意思_brutal在线翻译_英语_读音_用法_例句_海词词典 brutal
- birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典
birdnest是什么意思_birdnest在线翻译_英语_读音_用法_例句_海词词典 birdnest
- Python爬虫教程-16-破解js加密实例(有道在线翻译)
python爬虫教程-16-破解js加密实例(有道在线翻译) 在爬虫爬取网站的时候,经常遇到一些反爬虫技术,比如: 加cookie,身份验证UserAgent 图形验证,还有很难破解的滑动验证 js签 ...
- C++调用有道翻译API实现在线翻译之发声篇
大概半月前写了一篇博文:C++中使用Curl和JsonCpp调用有道翻译API实现在线翻译, 得到大家的热情捧场,有人看了文章说要是能发声不是更好,我觉得说的也是哈,能听到专家的标准发音,那该是多美的 ...
随机推荐
- 那些年我们追过的C#奇葩关键字——忐忑
说到中国的歌坛,不能光说张学友这种大咖吧,我看那些怪咖更给力,比如我们的龚琳娜童鞋,一首神曲<忐忑>唱的那叫不可收拾,而且听到的改编版本更多,每一次都是心怀忐忑,就像C#里的那些关键字 说 ...
- 20155224聂小益 2016-2017-2 《Java程序设计》第1周学习总结
20155224聂小益 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 第一章 第一章内容不是很多,主要介绍了Java发展历程与Java的使用平台. JVM: ...
- 20155322 2016-2017-2 《Java程序设计》第10周学习总结
20155322 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 第10周学习的主要内容Java和Android开发学习指南(第二版)(EPUBIT,Jav ...
- Wiki版产品需求---产品需求文档到底是谁的?产品到底是谁的?
在听了测试的一通唠叨之后,"内部实现一堆逻辑,只有一句话的需求文档","文档那么简单,我们怎么测试啊",心中突然想起来自己曾经干的一件当时觉得还不错的事情,但是 ...
- [hdu6051]If the starlight never fade-[欧拉函数+原根]
Description 传送门 Solution orz大佬yxq..本题神仙 设g为P的原根. 设$x=g^{a}$,$y=g^{b}$. 由于$(g^{a}+g^{b})^{i}\equiv (g ...
- day 3 模块
1.系统自带模块 xxx.py 文件 就是模块 ### 模块存放位置 In [1]: import os In [2]: os.__file__ Out[2]: '/usr/lib/python3. ...
- Entity Framework中执行Sql语句
如果想在EF框架中执行Sql语句,其实很简单,EF里面已经提供了相关的方法(此处使用的EF为EF4.1版本). EF中提供了两个方法,一个是执行查询的Sql语句SqlQue ...
- MAVEN相关文章
Maven入门指南① :Maven 快速入门及简单使用 Maven入门指南② :Maven 常用命令,手动创建第一个 Maven 项目 Maven入门指南③:坐标和依赖 Maven入门指南④:仓库 M ...
- Struts 2(二):使用Struts2
本文简单描述如何在Eclipse中使用使用Struts2,并介绍一下Struts2的配置文件 注:Struts2默认需要Java 5.0及其以上版本的运行环境支持,Web容器需要支持Servlet 2 ...
- Maven学习(四)-----Maven中央存储库
Maven中央存储库 当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载.首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源,如果没 ...