Give me the number
Time Limit:1000MS Memory Limit:65536K
Total Submit: Accepted:
Description
在英文里面,数字是以下面所给的方式写出来的(现只考虑小于10^9 的数字)。 数字 abc,def,ghi 记作 "[abc] million [def] thousand [ghi]"。这里 "[xyz] " 表示三位数xyz 。
如果abc = 0 ,那么 "[abc] million" 被省略,如果def = 0 ,那么 "[def] thousand" 也被省略,如果ghi = 0 ,那么 "[ghi] " 也被省略。如果整个数就是0,那么记作 "zero"。注意到,就算有多于一个的“million”或多于一个的“thousand”,单词 "million" 和 "thousand" 都是单数形式。
小于一千的数字是采用下面的方式表示。数字 xyz 记作 "[x] hundred and [yz] ”。( 如果 yz = 0 ,那么它就是 “[x] hundred”。否则,如果 y = 0 ,就应记作 “[x] hundred and [z]”。) 如果x = 0 , "[x] hundred and" 被省略。注意到 "hundred" 也是单数形式。
20以下的数字记作 "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen" 以及 "nineteen" 。
20 到 99 之间的数字用下面的方法表示。数字 xy 记作 "[x0] [y] ", 其中的“二十”到“九十”相应记作 "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", and "ninety" 。
例如,数字987,654,312记作 "nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve"
数字100,000,037记作 "one hundred million thirty seven", 数字1,000 记作 "one thousand"。
注意到:对于百万、千、百来说 "one" 是不会被省略的。
给出用英文表示的数字,请写出相应的阿拉伯数字形式的整数。
Input
有多个测试用例。输入的第一行是一个整数 T (1 <= T <= 1900) ,表示测试用例的个数。接下来是T 个测试用例。
每个测试用例占一行,是一串表示一个数字的英文单词。
Output
对应每行输入的英文数字,输出相应的阿拉伯数字整数,单独一行。假设所有整数都少于109 。
Sample Input
4
one
eleven
one hundred and two
two hundred million seven hundred and one thousand two hundred and ten
Sample Output
1
11
102
200701210
代码加解题思路如下:
#include<iostream>
#include<string>
#include<map>
using namespace std;
string str,temp;
map<string,int> m; //强大的map
//char m[][20]={,m["one",m["two",m["three",m["four",m["five",m["six",m["seven",m["eight",m["nine",m["ten",m["eleven",m["twelve",m["thirteen",m["fourteen",m["fifteen",m["sixteen",m["seventeen",m["eighteen",m["nineteen","twenty",m["thirty",m["forty",m["fifty",m["sixty",m["seventy",m["eighty",m["ninety",m["hundred",m["thousand",m["",m["billion"};
int main()
{
// freopen("E://data.in", "r", stdin);
m["zero"]=0;
m["one"]=1;
m["two"]=2;
m["three"]=3;
m["four"]=4;
m["five"]=5;
m["six"]=6;
m["seven"]=7;
m["eight"]=8;
m["nine"]=9;
m["ten"]=10;
m["eleven"]=11;
m["twelve"]=12;
m["thirteen"]=13;
m["fourteen"]=14;
m["fifteen"]=15;
m["sixteen"]=16;
m["seventeen"]=17;
m["eighteen"]=18;
m["nineteen"]=19;
m["twenty"]=20;
m["thirty"]=30;
m["forty"]=40;
m["fifty"]=50;
m["sixty"]=60;
m["seventy"]=70;
m["eighty"]=80;
m["ninety"]=90;
m["hundred"]=100;
m["thousand"]=1000;
m["million"]=1000000;
m["and"]=0; //打得好烦啊,打到后来才想起可以用offic工具。。。Orz
int cases;
int num;
int p;
int i;
int len;
int sum;
scanf("%d",&cases);
getchar();
while(cases--)
{
getline(cin,str); //string好像怎么用都不会太长的=_=
str=str+' '; //开始是想每个单词处理一次的,后来发现不会,应该是用sscanf()吧,但偷懒了
str=' '+str; //发现这样每个单词就是“temp ”了
num=0;
p=1;
sum=0;
len=str.length();
for(i=1;i<len;i++)
{
if(str[i]==' ')
{
temp=str.substr(p,i-p); //从p开始长度为i-p的串,最开始记成是begin和end了。555,记住,string.substr第二个参数是长度!!
p=i+1;
if(m[temp]>100)
{
num*=m[temp];
sum+=num;
num=0;
}
else if(m[temp]==100) //100很牛B!!
{
num*=100;
}
else
num+=m[temp];
}
}
sum+=num;
printf("%d/n",sum);
}
return 0;
}