题目描述
(洛谷链接)
题目描述
为了适应紧张的大学学习生活,小Z发愤图强开始复习巩固英语。
由于小Z对数学比较有好感,他首先复习了数词。小Z花了一整天的时间,终于把关于基数词的知识都搞懂了。于是小Z非常兴奋,决定出一些题目考考已经过了英语四级、人称英语帝的小 G。考法很简单:小Z给出某个整数 x 的英文写法,要求小D用阿拉伯数字写出x。
小Z会保证以下几点:
1、-999,999,999 ≤ x ≤ 999,999,999
2、题目中只会用到以下这些英文单词:negative, zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen,fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand, million
3、若 x 为负数,题目中第一个单词是 negative,否则任何时候都不会出现 negative 这个词。
4、由于小Z很牛 B,他不知道像 103 这样的数字要写成 one hundred and three 而是直接写成了 one hundred three,就是说小Z的所有题目中都没有写 and 这个词(尽管本应该是要写的),请你谅解。
5、除了第 4 点, 其他还是基本符合英语的语法规则的, 比如 1500 他会写成 one thousand five hundred 而不会写成 fifteen hundred。
小D拿到题目后不屑地说了一句:水题!写个程序么好了……
但是小D要出去玩(此时应该已经在千里之外爽玩了) ,这个任务就交给你了。
输入格式
一行,题目描述中所说的 x 的英文写法。
输出格式
一行, x 的阿拉伯数字写法。
输入输出样例
输入#1:six
输出#1:6
输入#2:negative seven hundred twenty nine
输出#2:-729
输入#3:one million one hundred one
输出#3:1000101
输入#4:eight hundred fourteen thousand twenty two
输出#4:814022
说明/提示
【数据规模】
对于100%的数据,-999,999,999 ≤ x ≤ 999,999,999
【时空限制】
0.1s/16M
代码、思路
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #include<iostream> using namespace std; int main(){ int ans=0,end=0; string tmp; while(cin>>tmp){ if(tmp=="negative"){ cout <<"-"; } if(tmp=="zero"){ ans+=0; } if(tmp=="one"){ ans+=1; } if(tmp=="two"){ ans+=2; } if(tmp=="three"){ ans+=3; } if(tmp=="four"){ ans+=4; } if(tmp=="five"){ ans+=5; } if(tmp=="six"){ ans+=6; } if(tmp=="seven"){ ans+=7; } if(tmp=="eight"){ ans+=8; } if(tmp=="nine"){ ans+=9; } if(tmp=="ten"){ ans+=10; } if(tmp=="eleven"){ ans+=11; } if(tmp=="twelve"){ ans+=12; } if(tmp=="thirteen"){ ans+=13; } if(tmp=="fourteen"){ ans+=14; } if(tmp=="fifteen"){ ans+=15; } if(tmp=="sixteen"){ ans+=16; } if(tmp=="seventeen"){ ans+=17; } if(tmp=="eighteen"){ ans+=18; } if(tmp=="nineteen"){ ans+=19; } if(tmp=="twenty"){ ans+=20; } if(tmp=="thirty"){ ans+=30; } if(tmp=="forty"){ ans+=40; } if(tmp=="fifty"){ ans+=50; } if(tmp=="sixty"){ ans+=60; } if(tmp=="seventy"){ ans+=70; } if(tmp=="eighty"){ ans+=80; } if(tmp=="ninety"){ ans+=90; } if(tmp=="hundred"){ ans*=100; } if(tmp=="thousand"){ ans*=1000; end+=ans; ans=0; } if(tmp=="million"){ ans*=1000000; end+=ans; ans=0; } } end+=ans; cout<<end; return 0; }
|
94行的注释建议大家好好看看理解一下,如果你不是很懂。这是其他题解没有解释的内容
踩过的坑
1.对hundred,thousand,million的处理错误
最初写代码时,对于hundred,thousand,million的处理是这么写的
1 2 3 4 5 6 7 8 9
| if(tmp=="hundred"){ ans*=100; } if(tmp=="thousand"){ ans*=1000 } if(tmp=="million"){ ans*=1000000; }
|
至于具体的为什么不能这样写,在正确代码的注释中已经解释过了。
2.最后没有写end+=ans
其他想说的
这道题的测试样例有50个
这道题其实挺考细心的,我最初一个单词敲错了……
题目描述的4,5也可以当作测试样例