The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemG:Give Me the Number

时间:2021-12-13 13:26:01

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971

题意:将输入的英文数字表达转化为阿拉伯数字。

 #include<bits/stdc++.h>
using namespace std; char aa[][]= {"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"
};
int bb[]= {,,,,,,,,,,,,,,,,,,,,,,,,,,,};
char cc[][];
int main() {
int t;
char s[];
scanf("%d",&t);
getchar();//先吃掉t后面的回车
while(t--) {
memset(s,,sizeof(s));
memset(cc,,sizeof(cc));
gets(s);
int num=,k=;
int len=strlen(s);
for(int i=; i<len; i++) {
if(i>=&&s[i]==' '&&s[i-]!=' ') {
num++;//在’ ‘前算上一个字符串
k=;
continue;
}
if(s[i]!=' ')
cc[num][k++]=s[i];//装入一个字符串
}
int sum1=,sum2=;
int j;
        //有前至后读入字符串
for(int i=; i<=num; i++) {
for(j=; j<; j++) {
if(strcmp(cc[i],aa[j])==) {
sum1+=bb[j];
break;
}
}
if(j=) {
if(strcmp(cc[i],"and")==)
continue;
if(strcmp(cc[i],"thousand")==) {
sum1*=;
sum2+=sum1;
sum1=;
} else if(strcmp(cc[i],"hundred")==)
sum1*=;
else if(strcmp(cc[i],"million")==) {
sum1*=;
sum2+=sum1;
sum1=;
}
}
}
printf("%d\n",sum2+sum1);
}
return ;
}//善于使用strcmp函数,其实想到就很简单了