您的位置:首页 > 其它

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

2016-02-20 12:48 411 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971

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

#include<bits/stdc++.h>
using namespace std;

char aa[30][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"
};
int bb[28]= {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};
char cc[300][20];
int main() {
int t;
char s[250];
scanf("%d",&t);
getchar();//先吃掉t后面的回车
while(t--) {
memset(s,0,sizeof(s));
memset(cc,0,sizeof(cc));
gets(s);
int num=0,k=0;
int len=strlen(s);
for(int i=0; i<len; i++) {
if(i>=1&&s[i]==' '&&s[i-1]!=' ') {
num++;//在’ ‘前算上一个字符串
k=0;
continue;
}
if(s[i]!=' ')
cc[num][k++]=s[i];//装入一个字符串
}
int sum1=0,sum2=0;
int j;
        //有前至后读入字符串
for(int i=0; i<=num; i++) {
for(j=0; j<28; j++) {
if(strcmp(cc[i],aa[j])==0) {
sum1+=bb[j];
break;
}
}
if(j=28) {
if(strcmp(cc[i],"and")==0)
continue;
if(strcmp(cc[i],"thousand")==0) {
sum1*=1000;
sum2+=sum1;
sum1=0;
} else if(strcmp(cc[i],"hundred")==0)
sum1*=100;
else if(strcmp(cc[i],"million")==0) {
sum1*=1000000;
sum2+=sum1;
sum1=0;
}
}
}
printf("%d\n",sum2+sum1);
}
return 0;
}//善于使用strcmp函数,其实想到就很简单了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: