POJ 2121

时间:2023-02-04 09:15:56

http://poj.org/problem?id=2121

一道字符串的转换的题目。

题意:就是把那个英文数字翻译成中文。

思路:首先打表,然后把每一个单独的单词分离出来,在组合相加相乘。

 #include <stdio.h>
#include <string.h> struct trans{
char eng[];
int num;
}s[]; int main(){
// freopen("in.txt","r",stdin);
strcpy(s[].eng,"zero"), s[].num = ;
strcpy(s[].eng,"one"), s[].num = ;
strcpy(s[].eng,"two"), s[].num = ;
strcpy(s[].eng,"three"), s[].num = ;
strcpy(s[].eng,"four"), s[].num = ;
strcpy(s[].eng,"five"), s[].num = ;
strcpy(s[].eng,"six"), s[].num = ;
strcpy(s[].eng,"seven"), s[].num = ;
strcpy(s[].eng,"eight"), s[].num = ;
strcpy(s[].eng,"nine"), s[].num = ;
strcpy(s[].eng,"ten"), s[].num= ;
strcpy(s[].eng,"eleven"), s[].num= ;
strcpy(s[].eng,"twelve"), s[].num= ;
strcpy(s[].eng,"thirteen"), s[].num= ;
strcpy(s[].eng,"fourteen"), s[].num= ;
strcpy(s[].eng,"fifteen"), s[].num= ;
strcpy(s[].eng,"sixteen"), s[].num= ;
strcpy(s[].eng,"seventeen"),s[].num= ;
strcpy(s[].eng,"eighteen"), s[].num= ;
strcpy(s[].eng,"nineteen"), s[].num= ;
strcpy(s[].eng,"twenty"), s[].num= ;
strcpy(s[].eng,"thirty"), s[].num= ;
strcpy(s[].eng,"forty"), s[].num= ;
strcpy(s[].eng,"fifty"), s[].num= ;
strcpy(s[].eng,"sixty"), s[].num= ;
strcpy(s[].eng,"seventy"), s[].num= ;
strcpy(s[].eng,"eighty"), s[].num= ;
strcpy(s[].eng,"ninety" ), s[].num= ;
strcpy(s[].eng,"hundred"), s[].num= ;
strcpy(s[].eng,"thousand"), s[].num= ;
strcpy(s[].eng,"million"), s[].num= ;
strcpy(s[].eng,"negative"); s[].num= -;
char a[];
while(gets(a))
{
if(strlen(a)==) break;
char tmp[]={};
int ans=,flog=,sum=,x=;
int len=strlen(a);
for(int i=,k=;i<len;i++)
{
if(a[i]!=' ') tmp[k++]=a[i];
if(a[i]==' '||i==len-){
k=;
flog=;
for(int m=;m<=;m++){
if(strcmp(s[m].eng,tmp)==){
if(m==) {
x=-;
continue;
}
if(m<=) sum+=s[m].num;
if(m==) sum*=s[m].num;
if(m>=) {
ans+=sum*s[m].num;
sum=;
}
break;
}
}
}
if(flog==){
memset(tmp,,sizeof(tmp));
flog=;
}
}
printf("%d\n",(ans+sum)*x);
}
return ;
}