G题,就是以前做过的字符串专题的差不多的题啊,就是多了个 “and ” 遇到 continue 即可。。。
#include<cstdio>
#include<cstring>
char text[35][20]={"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",// 0 ~ 10
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", // 11 ~ 20
"thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred", // 30 ~ 100 --- 21 ~ 28
"thousand", "million", "negative","and"// 1000 --- 29 , 1000 000 --- 30 , '-' --- 31,'and'---32
};
//无脑流,模拟题
char ori[20000],s[100][20];
int find(char *ss){
int i;
for(i=0;i<33;i++)
if(strcmp(text[i],ss)==0) break;
if(i==31) return -1;
else if(i==32) return -2;
else if(i>=0 && i<=20) return i;
else if(i>=21 && i<=28) return (i-20+2)*10;
else if(i==29) return 1000;
else if(i==30) return 1000000;
}
int main()
{
int i,t;
scanf("%d",&t);
while(t){
gets(ori);
if(strlen(ori)==0) continue;//没加这句居然会WA ,以后得注意加这句
t--;
long long ans=0;
memset(s,0,sizeof(s));
int tot=0,w=0;
for(i=0;i<(int)strlen(ori);i++){
if(ori[i]==' '){
tot++;
w=0;
}
else s[tot][w++]=ori[i];
}
int thous=1,milli=1,hund=1;
for(i=tot;i>=0;i--){
int num=find(s[i]);
if(num==-1) ans*=-1;
else if(num==-2) continue;
else if(num==1000000) {milli*=num;thous=1;hund=1;}
else if(num==1000) {thous*=num;hund=1;}
else if(num==100) hund*=num;
else ans+=num*(milli*thous*hund);
}
printf("%lld\n",ans);
}
return 0;
}