真假硬币谁重?

发布时间 2023-06-06 19:49:37作者: 网工嘟

枚举算法之真假硬币的是谁之假硬币的轻or重?

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 char Left[3][7];
 5 char Right[3][7];
 6 char result[3][7];
 7 bool isfake(char c,bool light) ;
 8 int main()
 9 {
10     int t;
11     cin>>t;
12     while(t--){
13         for(int i=0;i<3;++i)  cin>>Left[i]>>Right[i]>>result[i]; 
14          for(char c='A';c<='L';c++){
15             if(isfake(c,true))
16             {
17                 cout<<c<<"  is the counterfeit coin and it is light.";
18                 break;
19             }    
20             else if(isfake(c,false)){
21                 cout<<c<<"  is the counterfeit coin and it is light.";
22                 break;
23             }    
24         }
25     }
26 }
27 bool isfake(char c,bool light)
28 {
29     for(int i=0;i<3;++i)
30     {
31         char * pl,*pr;//
32         if (light)
33         {
34             pl =Left[i];
35             pr=Right[i];
36         }
37         else { //
38             pl =Right[i];
39             pr =Left[i];
40         }
41     switch(result[i][0]){
42         case 'u':
43             if(strchr(pr,c) == NULL)
44                 return false;
45             break;
46         case 'e':
47             if(strchr(pl,c)||strchr(pr,c))
48                 return false;
49             break;
50         case'd':
51             if(strchr(pl,c)==NULL)
52                 return false;
53             break;
54         } 
55     }
56     return true;
57 }
58 /*真假硬币 
59 12枚硬币,1枚假的硬币,不知轻重
60 要求:找出哪个是假的硬币,称过三次,假币是重的还是轻的
61 输入样例:
62 1
63 ABCD EFGH even
64 ABCI EFJK up
65 ABIJ EFGH even
66 输出样例:
67 K是假的硬币,是轻的。
68 K is the counterfeit coin and it is light
69 枚举法 
70 */