S - Counterfeit Dollar

Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins.
Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs
one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively.
By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.
Input
The first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A--L. Information on a weighing will be given by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.
Output
For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.
Sample Input
1 
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 
Sample Output
K is the counterfeit coin and it is light. 每一个
题意:天平找假币,输入n,表示要找n枚假硬币,每一个假硬币包含3行,每行前表示左盘,中表示右盘,后表示状态,up表示右盘高,down表示右盘低。even表示平衡。假硬币比真的重或轻,硬币表示A到L,重输出heavy 轻输出light.
模拟法有点麻烦,然后经过一番查找我发现了暴力枚举法,枚举A到L每一枚硬币,判断是否为假币。

1 ABCD EFGH even

ABCI EFJK up

ABIJEFGH even 我们用-1,表示轻币,1表示重币,如果天平两边有和枚举相同的便加上此时枚举的状态(初始为0).通过例子枚举为k时并且为1时,我们发现第一行两边0=0,但此时为even,类似的第二行为1>0,up,第三行为0=0,even,枚举为k并且为-1.第一行0=0even 第二行0>-1,up,第三行0=0,even.答案为k,-1.说明为假币时,必须保证状态0=0时为even,不等于时,倒向相应的状态一边。根据,具体代码理解
#include<iostream>
#include<algorithm>
using namespace std;
char b[2][10]={"light","heavy"};//保存状态 
char a[3][3][15];
int judge(char x,int y);
int main()
{
 int n,i,j;
 char k;
 cin>>n;
 while(n)
 {
  n--;
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   {
    cin>>a[i][j];
   }
  }
  for(k='A';k<='L';k++)
  {
   for(j=-1;j<2;j+=2)
   {
    if(judge(k,j))
    {
     cout<<k<<" is the counterfeit coin and it is "<<b[max(j,0)]<<"."<<endl;
    }
   }
  }
 }
}
int judge(char x,int y)
{
 int i,j,k;
 for(i=0;i<3;i++)
 {
  int result[2]={0};
  for(j=0;j<2;j++)
  {
   for(k=0;a[i][j][k];k++)
   {
    if(a[i][j][k]==x)
    {
     result[j]+=y;
    } 
   }
   }
   if(result[0]==result[1]&&a[i][2][0]!='e')
   {
    return 0;
   }
   if(result[0]<result[1]&&a[i][2][0]!='d')
   {
    return 0;
   }
   if(result[0]>result[1]&&a[i][2][0]!='u')
   {
    return 0;
   }
 }
 return 1;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值