Counterfeit Dollar
http://poj.org/problem?id=1013
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 const int INF=1<<28; 5 int main() 6 { 7 char rw[3][21],lw[3][21],vis[3][11]; 8 int ans[303],t; 9 scanf("%d",&t); 10 while(t--) 11 { 12 memset(ans,0,sizeof(ans)); 13 for (int i = 0; i < 3; i ++) 14 { 15 scanf("%s %s %s",lw[i],rw[i],vis[i]); 16 } 17 for (int i = 0; i < 3; i ++) 18 { 19 if(vis[i][0]=='e') 20 { 21 for (int j = 0; lw[i][j]!='\0'; j ++) 22 { 23 ans[lw[i][j]-'A'] = INF; 24 ans[rw[i][j]-'A'] = INF; 25 } 26 } 27 } 28 for (int i = 0; i < 3; i ++) 29 { 30 31 for (int j = 0; lw[i][j]!='\0'; j ++) 32 { 33 if (vis[i][0]=='u') 34 { 35 36 if(ans[lw[i][j]-'A'] != INF) 37 { 38 ans[lw[i][j]-'A']++; 39 } 40 if(ans[rw[i][j]-'A'] != INF) 41 { 42 ans[rw[i][j]-'A']--; 43 } 44 } 45 46 else if (vis[i][0]=='d') 47 { 48 49 if(ans[lw[i][j]-'A'] != INF) 50 { 51 ans[lw[i][j]-'A']--; 52 } 53 if(ans[rw[i][j]-'A'] != INF) 54 { 55 ans[rw[i][j]-'A']++; 56 } 57 } 58 else 59 break; 60 } 61 } 62 int max = 0,pos; 63 for (int i = 0; i <= 'L'-'A'; i ++) 64 { 65 if (ans[i]!=INF) 66 { 67 if(fabs(ans[i]) > max) 68 { 69 max = fabs(ans[i]); 70 pos = i; 71 } 72 } 73 } 74 if(ans[pos] <= 0) 75 printf("%c is the counterfeit coin and it is light.\n",pos+'A'); 76 else 77 printf("%c is the counterfeit coin and it is heavy.\n",pos+'A'); 78 } 79 return 0; 80 81 }