foj1003

Problem 1003 Counterfeit Dollar

Accept: 998    Submit: 4440
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

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

1ABCD EFGH evenABCI EFJK upABIJ EFGH even

 Sample Output

K is the counterfeit coin and it is light.



题意不说了。。。。耐心看看,都看得懂。

思路说下,这个题目提供了一种很好的思路拓展,就是发现问题的隐含条件。这题最主要的隐含条件是如果天平出现了不平衡,那么其余没有放上来的钱币都是真的钱,这个条件是由题目条件仅有一个假币推理得到的。如果不考虑这一点,如果当前是不平衡就会使得没有处理过的钱币被标记为不平衡,而不能通过之前的平衡标记过滤掉这个操作。就会使得结果不正确。。。。。。嗯比如我,wa了一屏。各种标记定义写在代码里面。还有个隐含就是两边个数相等,毕竟如果不等,就是全真也不平衡。

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int N = 333;
char l[N],r[N],w[11];
int hash[N];
//1:重的
//2:没处理过
//0:真的
//-1:轻的
void setEven(int n,bool op)
{//1:置天平上钱币平衡

//0:置天平下钱币平衡
  if(op)
  for(int i = 0;i < n;i ++)
    hash[l[i]] = hash[r[i]] = 0;
  else 
  {
    for(int i = 'A';i <= 'L';i ++)
    {
      bool d = 1;//查找不在天平上的钱币
      for(int j = 0;j < n;j ++)
        if(l[j] == i || r[j] == i){d = 0;break;}
      if(d)hash[i] = 0;
    }      
  }
}
//0:l
//1:r
int jud(bool op,char ch)
{
  if(w[0] == 'u'){
    if(hash[ch] + (op?-1:1) == 0)
      hash[ch] = 0;
    if(hash[ch] == 2)
      hash[ch] = op?-1:1;
  }else if(w[0] == 'd'){
    if(hash[ch] + (op?1:-1) == 0)
      hash[ch] = 0;
    if(hash[ch] == 2)
      hash[ch] = op?1:-1;
  }
}
void solve()
{
  int len = strlen(l);
  if(w[0] == 'e')setEven(len,1);
  else{
    setEven(len,0);
    for(int j = 0;j < len;j ++)
      jud(0,l[j]);
    for(int j = 0;j < len;j ++)
      jud(1,r[j]);
  }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T --)
    {
      for(int i = 'A';i <= 'L';i ++)
        hash[i] = 2;//标记为处理
      for(int i = 0;i < 3;i ++)
      {
        scanf("%s%s%s",l,r,w);
        solve();
      }
      char ch;//查找假币标记
      for(int i = 'A';i <= 'L';i ++)
        if(hash[i]!=2 && hash[i]){
          ch = i;
          break;
        }
      printf("%c is the counterfeit coin and it is ",ch);
      if(hash[ch] == -1)puts("light.");
      else puts("heavy.");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值