http://poj.org/problem?id=2121
一道字符串的转换的题目。
题意:就是把那个英文数字翻译成中文。
思路:首先打表,然后把每一个单独的单词分离出来,在组合相加相乘。
1 #include <stdio.h> 2 #include <string.h> 3 4 struct trans{ 5 char eng[10]; 6 int num; 7 }s[40]; 8 9 int main(){ 10 // freopen("in.txt","r",stdin); 11 strcpy(s[0].eng,"zero"), s[0].num = 0; 12 strcpy(s[1].eng,"one"), s[1].num = 1; 13 strcpy(s[2].eng,"two"), s[2].num = 2; 14 strcpy(s[3].eng,"three"), s[3].num = 3; 15 strcpy(s[4].eng,"four"), s[4].num = 4; 16 strcpy(s[5].eng,"five"), s[5].num = 5; 17 strcpy(s[6].eng,"six"), s[6].num = 6; 18 strcpy(s[7].eng,"seven"), s[7].num = 7; 19 strcpy(s[8].eng,"eight"), s[8].num = 8; 20 strcpy(s[9].eng,"nine"), s[9].num = 9; 21 strcpy(s[10].eng,"ten"), s[10].num= 10; 22 strcpy(s[11].eng,"eleven"), s[11].num= 11; 23 strcpy(s[12].eng,"twelve"), s[12].num= 12; 24 strcpy(s[13].eng,"thirteen"), s[13].num= 13; 25 strcpy(s[14].eng,"fourteen"), s[14].num= 14; 26 strcpy(s[15].eng,"fifteen"), s[15].num= 15; 27 strcpy(s[16].eng,"sixteen"), s[16].num= 16; 28 strcpy(s[17].eng,"seventeen"),s[17].num= 17; 29 strcpy(s[18].eng,"eighteen"), s[18].num= 18; 30 strcpy(s[19].eng,"nineteen"), s[19].num= 19; 31 strcpy(s[20].eng,"twenty"), s[20].num= 20; 32 strcpy(s[21].eng,"thirty"), s[21].num= 30; 33 strcpy(s[22].eng,"forty"), s[22].num= 40; 34 strcpy(s[23].eng,"fifty"), s[23].num= 50; 35 strcpy(s[24].eng,"sixty"), s[24].num= 60; 36 strcpy(s[25].eng,"seventy"), s[25].num= 70; 37 strcpy(s[26].eng,"eighty"), s[26].num= 80; 38 strcpy(s[27].eng,"ninety" ), s[27].num= 90; 39 strcpy(s[29].eng,"hundred"), s[29].num= 100; 40 strcpy(s[30].eng,"thousand"), s[30].num= 1000; 41 strcpy(s[31].eng,"million"), s[31].num= 1000000; 42 strcpy(s[28].eng,"negative"); s[28].num= -1; 43 char a[200]; 44 while(gets(a)) 45 { 46 if(strlen(a)==0) break; 47 char tmp[50]={0}; 48 int ans=0,flog=0,sum=0,x=1; 49 int len=strlen(a); 50 for(int i=0,k=0;i<len;i++) 51 { 52 if(a[i]!=‘ ‘) tmp[k++]=a[i]; 53 if(a[i]==‘ ‘||i==len-1){ 54 k=0; 55 flog=1; 56 for(int m=0;m<=31;m++){ 57 if(strcmp(s[m].eng,tmp)==0){ 58 if(m==28) { 59 x=-1; 60 continue; 61 } 62 if(m<=27) sum+=s[m].num; 63 if(m==29) sum*=s[m].num; 64 if(m>=30) { 65 ans+=sum*s[m].num; 66 sum=0; 67 } 68 break; 69 } 70 } 71 } 72 if(flog==1){ 73 memset(tmp,0,sizeof(tmp)); 74 flog=0; 75 } 76 } 77 printf("%d\n",(ans+sum)*x); 78 } 79 return 0; 80 }
时间: 2024-10-10 13:32:55