这个题主要是判断硬币真假,可能轻可能重,称三次,要输出哪枚是假币,还要输出是重的还是轻的,所以最主要的是标记变量
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<iostream> 5 using namespace std ; 6 7 int main() 8 { 9 int n ; 10 while(cin>>n ) 11 { 12 int a['L'+3] ; 13 for(int k = 1 ; k <= n ; k++) 14 { 15 memset(a,0,sizeof(a)) ; //没被标记 16 char ch[50],sh[50] ,ju[50]; 17 for(int j = 1 ; j <= 3 ; j++) 18 { 19 cin>>ch>>sh>>ju; 20 if(strcmp(ju,"even")==0) 21 { 22 for(int i = 0 ; i < strlen(ch) ; i++) 23 { 24 a[ch[i]] = 1000 ;//绝对真币为-1 25 a[sh[i]] = 1000 ; 26 } 27 } 28 else if(strcmp(ju,"up") == 0) 29 { 30 for(int i = 0 ; i < strlen(ch) ; i++) 31 { 32 if(a[ch[i]] != 1000) 33 a[ch[i]]++;// 34 if(a[sh[i]] != 1000) 35 a[sh[i]]-- ; 36 } 37 } 38 else if(strcmp(ju,"down") == 0) 39 { 40 for(int i = 0 ; i < strlen(ch) ; i++) 41 { 42 if(a[ch[i]] != 1000) 43 a[ch[i]] --; 44 if(a[sh[i]] != 1000) 45 a[sh[i]]++ ; 46 } 47 } 48 } 49 int max = 0 ; 50 char count ; 51 for(int j = 'A' ; j <= 'L' ; j++) 52 { 53 if(a[j] == 1000) 54 continue ; 55 if( fabs(a[j]) >= max) 56 { 57 max = fabs(a[j]) ; 58 count = j ; 59 } 60 } 61 cout<<count<< " is the counterfeit coin and it is "; 62 if(a[count] > 0) 63 cout<<"heavy."<<endl ; 64 else 65 cout<< "light."<<endl ; 66 } 67 } 68 return 0 ; 69 }