Give Me the Number
ZOJ - 2971Numbers 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
题意很长,但是只看样例就可以知道题意,就给给你数字的英文,转换成数字,数字保证小于1e9,也就是单位最大只会用到million。
思路是分段,根据英文记录数字的特点,三个数三个数一组,最大情况下我们只需要存三组,每组后面加不同的0即可最后加起来
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
map<string,int>num,dan;
void init(){
string s1[] = {"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"};
string s2[] = {"hundred","thousand","million"};
for(int i = 0; i < 20; i++){
num[s1[i]] = i;
}
for(int i = 20, j = 20; i < 100; i += 10,j++){
num[s1[j]] = i;
}
dan[s2[0]] = 100;
dan[s2[1]] = 1000;
dan[s2[2]] = 1000000;
}
int main(){
int t;
init();
scanf("%d",&t);
getchar();
string s;
while(t--){
int a[3] = {0};
int cnt = 0;
getline(cin,s);
for(int i = 0; i < s.length(); i++){
string tmp = "";
while(s[i] != ' ' && i < s.length()){
tmp += s[i];
i++;
}
if(num.count(tmp)){
a[cnt] += num[tmp];
}
else if(dan.count(tmp)){
a[cnt] *= dan[tmp];
if(tmp != "hundred") cnt++;
}
}
printf("%d\n",a[0]+a[1]+a[2]);
}
return 0;
}