问题描述
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.
输入
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.
输出
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.
样例输入
3 one eleven one hundred and two
样例输出
1 11 102
分析:我本来打算第二题就是这道的,然而嫌麻烦,写到一半就放弃了,去AC其他题了。后来挺后悔的,我的first blood啊,呜呜············这道题也没什么好说的,其实是蛮简单的。遇到一百,乘以一百,遇到一千,乘以一千,把每位加起来就行了。
CODE:
<span style="font-size:24px;">#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
struct node{
string str;
int num;
}n[1005];
int main()
{
string tmp1[20]={"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen","fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen"};
for(int i=0;i<20;i++){
n[i].str=tmp1[i];
n[i].num=i;
}
string tmp2[10]={ "","","twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty","ninety"};
for(int i=2;i<10;i++){
n[i*10].str=tmp2[i];
n[i*10].num=i*10;
}
int t;
cin>>t;
char ch;
ch=getchar();
while(t--){
int res=0,tmp=0;
char c[10005];
string s;
gets(c);
int len=strlen(c);
for(int i=0;i<=len;i++){
if(c[i]==' '||i==len){
bool flag=false;
for(int i=0;i<100;i++){
if(n[i].str==s){
tmp+=n[i].num;
flag=true;
break;
}
}
if(!flag){
if(s=="hundred")
tmp*=100;
else if(s=="million"){
tmp*=1000000;
res+=tmp;
tmp=0;
}
else if(s=="thousand"){
tmp*=1000;
res+=tmp;
tmp=0;
}
}
// cout<<res<<endl;
s.erase();
}
else
s+=c[i];
}
cout<<res+tmp<<endl;
}
return 0;
}</span>