Give Me the Number

  Numbers in English are written down in the following way (only numbers less than 109are 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

解题思路:
  给出行数t,之后t行每行给出一个用英文描述的数字,要求输出这个数字。

  这里从头开始记录这个数字,每记录一个数字就将它加入答案,根据题目语法以百万"million",千"thousand"和百"hundred"这几个在英文中有特殊表示形式的单词为标值,每次遇到这些标值就将目前的答案乘以标志数,如果是"million"或"thousand"("hundred"不用)从0开始重新记录下一个标值前的数字,一直记录到字符串的最后一个单词。

样例分析:

nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve

nine: 9 + 0 = 9

hundred: 9 * 100 = 900

eighty :80 + 900 = 980

and :0 + 980 = 980

seven :7 + 980 = 987

million :987  * 1000000 = 987000000  (遇到million重新从0开始记录)

six : 6 + 0 = 6

hundred : 6 * 100 = 600

and : 0 + 600 = 600

fifty : 50 + 600 = 650

four : 4 + 650 = 654

thousand : 654 * 1000 + 987000000 = 987654000  (遇到thousand重新从0开始记录)

three : 0 + 3 = 3

hundred : 3 * 100 = 300

and : 0 + 300 = 300;

twelve : 12 + 300 = 312;

987654000 + 312 = 987654312

  知道解法后就要思考怎样实现它,由于每一行为一个数字,输入时我们用一个getline之间获得一行存入string型的变量str中,用头文件sstream下的istringstream可以以空格为界读取str中的每个单词,将获取的每个单词计入string型变量temp中,并根据temp的内容进行操作,我们可以用map来建立每个(非"million","thousand","hundred")单词和其对应数字的关系。

AC代码

 #include<bits/stdc++.h>
using namespace std;
map<string, int> m;
string s1[] = {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
string s2[] = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
string s3 = "and";
string str, temp;
int main(){
for(int i = ; i < ; i++){ //建立0~19的映射
m[s1[i]] = i;
}
for(int i = ; i < ; i++){ //建立20,30……,90的映射
m[s2[i]] = + i * ;
}
m[s3] = ; //如果遇到and不需要操作,就将and映射为0即可
int t; //行数
scanf("%d", &t);
getchar(); //吸收换行符
while(t--){
getline(cin,str); //获取一行
istringstream cinstr(str);
int ans = , num = ; //ans记录答案,temp记录当前数字
while(cinstr >> temp){ //在str中读取单词
if(temp == "million"){ //遇到million乘以1000000并从0开始重新记录
ans += num * ;
num = ;
}else if(temp == "thousand"){ //遇到thousand乘以1000并从0开始重新记录
ans += num * ;
num = ;
}else if(temp == "hundred"){ //遇到hundred乘以100
num *= ;
}else{
num += m[temp]; //记录数字
}
}
ans += num; //将最后记录的数字加入答案
printf("%d\n", ans);
}
return ;
}

ZOJ 2971 Give Me the Number的更多相关文章

  1. ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)

    ZOJ 2971 Give Me the Number 题目 ZOJ 2311 Inglish-Number Translator 题目 //两者题目差不多,细节有点点不一样,因为不是一起做的,所以处 ...

  2. ZOJ 2971 Give Me the Number (模拟,字符数组的清空+map)

    Give Me the Number Time Limit: 2 Seconds      Memory Limit: 65536 KB Numbers in English are written ...

  3. zoj 4099 Extended Twin Composite Number

    Do you know the twin prime conjecture? Two primes  and  are called twin primes if . The twin prime c ...

  4. ZOJ 2132 The Most Frequent Number (贪心)

    题意:给定一个序列,里面有一个数字出现了超过 n / 2,问你是哪个数字,但是内存只有 1 M. 析:首先不能开数组,其实也是可以的了,后台数据没有那么大,每次申请内存就可以过了.正解应该是贪心,模拟 ...

  5. ZOJ - 2132:The Most Frequent Number(思维题)

    pro:给定N个数的数组a[],其中一个数X的出现次数大于N/2,求X,空间很小. sol:不能用保存数组,考虑其他做法. 由于出现次数较多,我们维护一个栈,栈中的数字相同,所以我们记录栈的元素和个数 ...

  6. nenu contest3 The 5th Zhejiang Provincial Collegiate Programming Contest

    ZOJ Problem Set - 2965 Accurately Say "CocaCola"!  http://acm.zju.edu.cn/onlinejudge/showP ...

  7. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  8. 整体二分(SP3946 K-th Number ZOJ 2112 Dynamic Rankings)

    SP3946 K-th Number (/2和>>1不一样!!) #include <algorithm> #include <bitset> #include & ...

  9. ZOJ 3622 Magic Number 打表找规律

    A - Magic Number Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Subm ...

随机推荐

  1. Linux服务器使用XShell上传下载文件

    在学习Linux过程中,我们常常需要将本地文件上传到Linux主机上,这里简单记录下使用Xsheel工具进行文件传输 1:首先连接上一台Linux主机 2:输入rz命令,看是否已经安装了lrzsz,如 ...

  2. Spring学习(五)——集成MyBatis

    本篇我们将在上一篇http://www.cnblogs.com/wenjingu/p/3829209.html的Demo程序的基础上将 MyBatis 代码无缝地整合到 Spring 中. 数据库仍然 ...

  3. 获取outlook联系人寻呼字段

    这称不上一篇技术文. 这边记录解决一个问题的过程和感受.这种感觉每个搞IT的人或多或少都感受过,是程序人独有的快乐之一.只是大部分人没有将这种感觉记录下来.但是当你记录时,这种感觉也早已消失. 需求: ...

  4. 升级到Sharepoint 2013后页面打开速度慢

    这个问题现在有了一些新的发现. 首先,我找到了重现客户那里出现的那个复杂SQL语句的方法.这个现象其实是这样的: 当WebApplication的“List View Threshold” 数量大于 ...

  5. Gogland使用 - 非常简单查看Go语言源代码全貌!

    Go语言也支持面向对象开发,不过和以往我们所使用的面向对象开发还是有不同,Go语言主张组合方式形成类的概念,在Go语言中,结构起到很大作用,如果用结构组合字段和方法,那么单纯在源代码中看,真的是费时费 ...

  6. BZOJ3210: 花神的浇花集会(坐标系变换)

    题面 传送门 题解 坐标系变换把切比雪夫距离转化为曼哈顿距离 那么对于所有的\(x\)坐标中,肯定是中位数最优了,\(y\)坐标同理 然而有可能这个新的点不合法,也就是说不存在\((x+y,x-y)\ ...

  7. matlab中显示灰阶图像

    matlab的数据源文件中400张图片,每张图片是一个112*92的矩阵表示,而400张图片存储在一个cell数组ime中,显示第一张图片,指令是: colormap(gray) imagesc(im ...

  8. 【招聘】滴滴滴~ i春秋内推直通车来咯,帮你找工作!

    凑是这么简单粗暴,i春秋冬日特享福利!虽然金九银十已经过去,但素想换工作想找工作的小哥哥小姐姐看过来! [职位方向]渗 透 测 试.代 码 审 计.安全开发.病毒分析.风险控制.安全运维.....任何 ...

  9. AngularJS入门讲解4:多视图,事件绑定,$resource服务讲解

    上一课,大家知道,手机详细模板我们没有写出来,使用的是一个占位模板. 这一课,我们先实现手机详细信息视图,这个视图会在用户点击手机列表中的一部手机时被显示出来. 为了实现手机详细信息视图,我们将会使用 ...

  10. [Objective-C语言教程]类型转换(20)

    类型转换是一种将变量从一种数据类型转换为另一种数据类型的方法. 例如,如果要将long值存储到简单整数(int)中,则可以将long类型转换设置为int.使用强制转换运算符将值从一种类型转换为另一种类 ...