Give Me the Number ZOJ - 2971

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.


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


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<sstream>
#include<map>
using namespace std;
typedef long long ll;

map<string,int> is_num;
int num[5];
string digit[30]={"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string d2[10]={"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty","ninety"};

void init(){
	for(int i=0;i<=19;i++){
		is_num[digit[i]]=i;
	}
	for(int i=0;i<=7;i++){
		is_num[d2[i]]=(i+2)*10;
	}
}

int is_class(string s){
	if(s=="thousand"||s=="million") return 1;
	return 0;
}

int get_class(string s){
	if(s=="thousand") return 2;
	if(s=="million") return 3;
}

int get_num(string s){
	return is_num[s];
}
char ch[1010];
/*
说实话,一点难写,主要是几个点,一个就是我不想写这种模拟题,在一个就是
我认为这道题可能会很难做,有些极端的样例,导致我们会很难受,
所以呢,我想着坐后面的题,一个就是分割符号,我当时不是很会找,还是一个空格,
然后我们再用string,就可以了,我想中间用stringstream但是,一直WA,不知道是
什么情况,就是很难受,感觉有时候,自己想的东西,不一定是最有效的,还是C
的处理可能才是最好的,毕竟自己熟悉这种环境 
*/
int main(){
	init();
	int T;
	scanf("%d",&T);
	getchar();
	while(T--){
		memset(num,0,sizeof(num));
		gets(ch);
		int len=strlen(ch);
		ch[len]=' ';
		int pre=0;
		string word;
		for(int i=0;i<=len;i++){
			if(ch[i]==' '){
				if(word=="hundred"){
					pre*=100;
				}
				else if(is_class(word)){
					int ct=get_class(word);
					num[ct]=pre;
					pre=0;
				}
				else
					pre+=get_num(word);
				word.clear();
			}
			else{
				word+=ch[i];
			}
		}
		num[1]=pre;
		int ans=0;
		for(int i=3;i>=1;i--){
			ans=ans*1000+num[i];
		}
		cout<<ans<<endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值