题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=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<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
int n,y,c,ans,i,j,k;
int s1[35]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90,100,1000,1000000};
char da[180];
char s2[10];
char s[35][10]={"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","hundred","thousand","million"};
scanf("%d",&n);
getchar();
while(n--)
{
gets(da);
c=0;ans=0;y=0;
for(i=0,j=0;i<=strlen(da);i++)
{
if(da[i]!=' '&& da[i]!='\0')
{
s2[j++]=da[i];
}
else
{
s2[j]='\0';
if(strcmp(s2,"and")==0)
{
j=0;
continue;
}
for(k=0;k<33;k++)
{
if(strcmp(s2,s[k])==0)
{
if(s1[k]!=100 && s1[k]!=1000 && s1[k]!=1000000)
ans+=s1[k];
else if(s1[k]==100)
ans*=s1[k];
else {y+=ans*s1[k];ans=0;}
}
}
j=0;
}
}
y+=ans;
printf("%d\n",y);
}
return 0;
}
本文介绍了一种将英文描述的数字转换为实际数值的算法实现。通过解析特定的英语数字表述规则,该算法能够准确地识别并计算出所对应的整数。文章详细解释了不同数量级数字的英语表达方式,并提供了一个具体的编程实例。
677

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



