HDU1228 字符串处理
题意:有a+b计算结果
View Code
1 #include<stdio.h> 2 #include<string> 3 #include<stdlib.h> 4 int mp( char a[] ){ 5 if( strcmp(a,"zero")==0 ) 6 return 0; 7 if( strcmp(a,"one")==0 ) 8 return 1; 9 if( strcmp(a,"two")==0 ) 10 return 2; 11 if( strcmp(a,"three")==0 ) 12 return 3; 13 if( strcmp(a,"four")==0 ) 14 return 4; 15 if( strcmp(a,"five")==0 ) 16 return 5; 17 if( strcmp(a,"six")==0 ) 18 return 6; 19 if( strcmp(a,"seven")==0 ) 20 return 7; 21 if( strcmp(a,"eight")==0 ) 22 return 8; 23 if( strcmp(a,"nine")==0 ) 24 return 9; 25 } 26 int main(){ 27 /* 28 mp["zero"]=0; 29 mp["one"]=1; 30 mp["two"]=2; 31 mp["three"]=3; 32 mp["four"]=4; 33 mp["five"]=5; 34 mp["six"]=6; 35 mp["seven"]=7; 36 mp["eight"]=8; 37 mp["nine"]=9; 38 */ 39 char ss[8]; 40 int num[3]; 41 char a[1005]; 42 while( gets(a)!=NULL ){ 43 num[1]=num[2]=0; 44 int flag=1; 45 for( int i=0;a[i]!='=';i++ ){ 46 if( a[i]==' ' ) 47 continue; 48 if( a[ i ]=='+' ){ 49 flag++; 50 continue; 51 } 52 int cnt=0; 53 //ss.clear(); 54 for( int j=i;;j++ ){ 55 if( a[j]<'a'||a[j]>'z' ){ 56 j--; 57 i=j; 58 //ss.at(cnt)='\0'; 59 ss[cnt]='\0'; 60 break; 61 } 62 //ss.at(cnt)=a[j]; 63 ss[ cnt ]=a[j]; 64 cnt++; 65 } 66 if( num[flag]==0 ) num[flag]+=mp(ss); 67 else { 68 num[flag]*=10; 69 num[flag]+=mp(ss); 70 } 71 } 72 if( num[1]+num[2]==0 ) 73 break; 74 printf("%d\n",num[1]+num[2]); 75 } 76 return 0; 77 }
keep moving...