题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1970
注意:简单的模拟题一定要仔细
源代码:
#include<string.h>
#include<stdio.h>
int get(char st[ ])
{
if (strcmp(st,"and")==0) return -1;
if (strcmp(st,"hundred")==0) return 100;
if (strcmp(st,"million")==0) return 1000000;
if (strcmp(st,"thousand")==0) return 1000;
if (strcmp(st,"zero")==0) return 0;
if (strcmp(st,"one")==0) return 1;
if (strcmp(st,"two")==0) return 2;
if (strcmp(st,"three")==0) return 3;
if (strcmp(st,"four")==0) return 4;
if (strcmp(st,"five")==0) return 5;
if (strcmp(st,"six")==0) return 6;
if (strcmp(st,"seven")==0) return 7;
if (strcmp(st,"eight")==0) return 8;
if (strcmp(st,"nine")==0) return 9;
if (strcmp(st,"ten")==0) return 10;
if (strcmp(st,"eleven")==0) return 11;
if (strcmp(st,"twelve")==0) return 12;
if (strcmp(st,"thirteen")==0) return 13;
if (strcmp(st,"fourteen")==0) return 14;
if (strcmp(st,"fifteen")==0) return 15;
if (strcmp(st,"sixteen")==0) return 16;
if (strcmp(st,"seventeen")==0) return 17;
if (strcmp(st,"eighteen")==0) return 18;
if (strcmp(st,"nineteen")==0) return 19;
if (strcmp(st,"twenty")==0) return 20;
if (strcmp(st,"thirty")==0) return 30;
if (strcmp(st,"forty")==0) return 40;
if (strcmp(st,"fifty")==0) return 50;
if (strcmp(st,"sixty")==0) return 60;
if (strcmp(st,"seventy")==0) return 70;
if (strcmp(st,"eighty")==0) return 80;
if (strcmp(st,"ninety")==0) return 90;
}
int main()
{
char a[5000];
char mid[20];
int num[100];
int test;
int i,j,k;
char c;
scanf("%d",&test);
scanf("%c",&c);
int flag;
int sign[3]; //标记数组,标记千位和百万位的个数
while(test--)
{
memset(num,0,sizeof(num));
memset(sign,0,sizeof(sign));
gets(a);
//scanf("%c",&c);
k=0;
for(i=0;a[i]!='\0';)
{
flag=0;
j=0;
while(a[i]==' '&&a[i]!='\0') i++;
while(a[i]!=' '&&a[i]!='\0')
{
flag=1;
mid[j++]=a[i++];
}
if(flag)
{
mid[j]='\0';
num[k++]=get(mid);
if(num[k-1]==1000000) sign[0]++;
if(num[k-1]==1000) sign[1]++;
}
}
//for(i=0;i<k;i++)
// printf("%d\n",num[i]);
int ans=0;
int mid=0;
for(i=0;i<k;i++)
{
if(num[i]==-1) continue;
if(num[i]==1000000)
{
mid*=1000000;
ans+=mid;
mid=0;
sign[0]--;
}
else if(num[i]==1000)
{
mid*=1000;
sign[1]--;
if(sign[0]==0)
{
ans+=mid;
mid=0;
}
}
else if(num[i]==100)
{
mid*=100;
if(sign[0]==0&&sign[1]==0)
{
ans+=mid;
mid=0;
}
}
else mid+=num[i];
//cout<<mid<<" Mid "<<endl;
//cout<<ans<<" ANs "<<endl;
}
ans+=mid;
printf("%d\n",ans);
}
}