Said coin (enum) - learning algorithm

problem

There are 12 coins. Of which 11 true coins and a fake coins. By weight of a genuine bill and counterfeit different
but does not know counterfeit or lighter weight than the true coin. Now, with a balance that three of these coins
tell you the results known, please identify counterfeit and identify counterfeit money is light or heavy (to ensure data will be able to find out)
Input
The first line is the number of test data sets.
Each group has three rows of data, each row represents a weighing result. Coins labeled AL. Each weighing result represented by three string separated by a space:
the right balance of coins placed in the balance of the coin is placed on the left balance. Wherein the equilibrium state with "up", "down", or "even" represents, respectively,
high right, and lower right balance. The number of coins left and right balance is always equal.
The output of
which is a reference output counterfeit coin, and that it is lighter weight than the true coin.

SAMPLE INPUT

3
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even

Sample Output

K is the counterfeit coin and it is light

Problem-solving ideas:

For every coin assume that he is the light, so to see whether the weighing result. If so, the problem that is solved. If it does not assume that he is heavy, to see whether the weighing result. Try them all coins, be able to find special coins

Code:

#incude<iostream>
#include<cstring>
using namespace std;
char Left[3][7];//天平左边硬币
char Right[3][7];//天平右边硬币
char result[3][7];//结果
bool IsFake(char c,bool right);
//light为真表示假设假币为轻,否则表示假设假币围为重
int main(){
 int t;
 cin>>t;
 while(t--){
 for(int i=0;i<3;++i)cin>>Left[i]>>Right[i]>>result[i];
 for(char c='A';c<'L';c++){//枚举假的硬币,从A-L
  if(IsFake(c,true)){
  cout<<c<<"is the counterfeit coin and it is the light.\n";
  break;
  }
  else if(IsFake(c,false)){
  cout<<c<<"is the counterfeit coin and it is the heavy.\n";
  break;
  }
      }
 } 
 bool ISFake(char c,bool light)
//light为真表示假币为轻,否则假币为重
{
for(int i=0;i<3;++i){
 char *pLeft,*pRight;//指向天平两边的字符串
 if(light){
  pLeft=Left[i];
  pRight=Right[i];
 }
 else{//如果假设假币是重的,则把称量结果左右对换
  pLeft=Right[i];
  pRight=Left[i];
 }
 switch(result[i][0]){//天平右边的情况
 case'u'://假币为轻的情况下,天平右边翘起来
  if(strchr(pRight,c)==NULL)
   return false;
  break;
 case'e':
  if(strchr(pLeft,c)||strchr(pRight,c))
   return false;
  break;
 case'd':
  if(strchr(pLeft,c)==NULL)
   return false;
  break;
 }
}
return 0;
}
Published 72 original articles · won praise 133 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_45822638/article/details/104994018