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实现在线翻译, 得到大家的热情捧场,有人看了文章说要是能发声不是更好,我觉得说的也是哈,能听到专家的标准发音,那该是多美的 ...
随机推荐
- 20155316 实验四 《Android程序设计》
实验1 实验内容 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章: ...
- 20155327 2016-2017-2 《Java程序设计》第一周学习总结
20155327 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 浏览教材,根据自己的理解每章提出一个问题 1.JAVA SE中JVM,JRE与JDK分别是什 ...
- 20155329实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 1实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 2. 初步掌握单元测试 ...
- [arc082F]Sandglass
Description 传送门 Solution 这题是真的666啊... 以下是本题最关键最关键的结论:如果ai<=aj,则在某个时间t,前者的A中沙子克数(记为t(ai))一定大于等于t(a ...
- day 3 局部变量 全局变量
1.局部变量 2.全局变量(死歌的大招)函数前面声明的都是全局变量 3.全局变量和局部变量的区别 1)老方法 def get_temper(): temper = 33 return temper d ...
- OpenCL入门:(一:Intel核心显卡OpenCL环境搭建)
组装的电脑没带独立显卡,用的是CPU自带的核显,型号是Intel HD Graphics 530,关于显卡是否可以使用OpenCL,可以下载GPU-Z软件查看. 本文在Windows 10 64位系统 ...
- js显示对象所有属性和方法的函数
function ShowObjProperty2( obj ) { // 用来保存所有的属性名称和值 var attributes = '' ; var methods = '' // 开始遍历 f ...
- PHP手动环境搭建之WAMP
第一步:安装apache程序 首先需要去Apache官网下载Apache2.4(http://httpd.apache.org/download.cgi),操作如下图所示: 下载完成后把它解压出来,然 ...
- PHP核心技术——魔术方法
魔术方法: 魔术方法是以两个下画线开头.具有特殊作用的一些方法,可以看做PHP的"语法糖". set和get方法: class Account{ private $user=1; ...
- 基于preteus的1602液晶显示器的学习(LM016L)
(证明学过,以示纪念) 所谓1602就是每行可以显示16个字符,可以显示两行.1602液晶在工业中使用比较广泛,其基本都采用的是HD44780控制器,或者兼容该指令集,因此基于HD44780写的控制程 ...