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
题意:根据数字的英文叫法,转化为数字
思路: 根据样例 nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve
#include<iostream>
#include<string>
#include<stdio.h>
#include<map>
#include<string.h>
using namespace std;
map<string,int>num,dan;
void init(){
string a[]={"zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string b[]={"twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"};
for(int i=0;i<20;i++)
num[a[i]]=i;
for(int i=20,j=0;i<=90;i+=10,j++)
num[b[j]]=i;
dan["hundred"]=100;
dan["thousand"]=1000;
dan["million"]=1000000;
}
int main(){
int T;
cin>>T;
init();
while(T--){
string str;
int arr[2]={0};
int ans=0;
while(cin>>str){
char ch=getchar();
if(num.count(str)){
ans+=num[str];
}
else if(str=="hundred"){
ans*=dan[str];
}
if(str=="million"){
arr[0]=ans*dan[str];
ans=0;
}
else if(str=="thousand"){
arr[1]=ans*dan[str];
ans=0;
}
if(ch=='\n') break;
}
cout<<arr[0]+arr[1]+ans<<endl;
}
}
/*
nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve
*/
本文介绍了一种将英语描述的数字转换为实际数值的方法。通过分析英语数字的构成规律,如million、thousand等单位的应用,实现了从英语描述到整数的有效转换。文章详细解释了算法实现,并提供了代码示例。
221

被折叠的 条评论
为什么被折叠?



