Give Me the Number(将字符串的值变为数)

本文介绍了一种将英文描述的数字转换为整数的算法实现。该算法能够处理从零到十亿之间的数字,并通过逆波兰表达式的思路进行解析。文中提供了完整的AC代码,包括输入输出样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

G - Give Me the Number
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

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
 
   
这题一开始根本没思路,最后看见了100,000,000中的逗号就突然来了灵感,就想到了下面的方法了。这题还借鉴了逆波兰序的思路吧!
 
   
AC代码:
 
   
 
   
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
#include<vector>
using namespace std;
#define T 550*550
typedef long long ll;
map<string,int> t;
void paly_table()
{
	t["zero"]=0, t["one"]=1, t["two"]=2, t["three"]=3;
	t["four"]=4, t["five"]=5, t["six"]=6, t["seven"]=7;
	t["eight"]=8, t["nine"]=9, t["ten"]=10,t["eleven"]=11;
	t["twelve"]=12, t["thirteen"]=13, t["fourteen"]=14;
	t["fifteen"]=15,t["sixteen"]=16,t["seventeen"]=17,t["eighteen"]=18;
	t["nineteen"]=19;
	 t["twenty"]=20,t["thirty"]=30,t["forty"]=40,t["fifty"]=50;
	 t["sixty"]=60,t["seventy"]=70,t["eighty"]=80,t["ninety"]=90;
	  t["millions"]=1000000,t["thousands"]=1000,t["hundreds"] = 100;
	  t["million"]=1000000,t["thousand"]=1000,t["hundred"] = 100;
}
int main()
{
#ifdef zsc
	freopen("input.txt","r",stdin);
#endif
	int n,i,k;
	cin >> n;
	string s;
	
	getline(cin,s);
	paly_table();
	while(n--)
	{
		vector<int> num;
	    vector<string> opp;
		getline(cin,s);
		string ss,str;
		k=0;
		for(i=0;s[i];++i){
			if(s[i]!=' ' ){
				ss += s[i];
			}
			else
			{
				if(ss=="and"){ss.clear();continue;}
				opp.push_back(ss);
				ss.clear();
			}
		}
		if(ss!=""){
				opp.push_back(ss);
				ss.clear();
		}
	/*	for(i=0;i<opp.size();++i){
			cout << opp[i] << " " ;
		}
		cout << endl;*/
		for(i=0,k=0;i<opp.size();++i){
			if(
				opp[i]=="million"||opp[i]=="thousand"
				||opp[i]=="millions"||opp[i]=="thousands"
			  )
			{
				num.push_back(k*t[opp[i]]);
				k = 0;
			}
			else
			{
				if(i+1<opp.size()&&(opp[i+1]=="hundred"||opp[i+1]=="hundreds")){
					k+= t[opp[i]]*t[opp[i+1]];i++;
				}
				else
				{
					k+=t[opp[i]];
				}
			}
		}
		if(k)num.push_back(k);
		int val=0;
		for(i=0;i<num.size();++i){
			/*cout << num[i] << "  ";*/
			val += num[i];
		}
	/*	cout << endl;*/
		cout << val <<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值