北大poj- 1013

Counterfeit Dollar

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 50515 Accepted: 15808

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

1 
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 

Sample Output

K is the counterfeit coin and it is light. 

Source

East Central North America 1998   分析: 很有意思的题目,人脑很好处理,但是用计算机来做就有点不知如何下手了。 拿到题目,列出方程,各种约,就能得到答案,但是这种方法,用计算机不好实现。后来突然想到,可以用“嫌疑人”的方法来做。 解题: 已知条件:1、只有一枚假币;2、假币可重可轻;3、一定可以根据已知的3次测量来判断出假币。 给每个硬币设置2个属性:1)是否为真;2)可疑度。 解题步骤: 先遍历所有even的case,设置even场景下的硬币都为真。 再遍历剩余的case, 当为up的时候,除去已知为真的硬币,对左硬币可疑度+1,对右硬币可疑度-1。 当为down的时候,除去已知为真的硬币,对右硬币可疑度+1,对左硬币可疑度-1。 结束后, 查找硬币中,可疑度绝对值最大的硬币,就是假币了。当可疑度为正,那么假币重;当可疑度为负,那么假币轻。   PPS:话说poj为啥会挂了呢。。。  

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 #define TRUE  (int)1
  6 #define FALSE (int)0
  7 
  8 #define CASE_NUM  3
  9 #define MAX_COIN_NUM 12
 10 
 11 typedef int BOOL;
 12 
 13 typedef struct
 14 {
 15     int  strLen;
 16     char str1[MAX_COIN_NUM+1];
 17     char str2[MAX_COIN_NUM+1];
 18     char delta[5];
 19 }WeightCase;
 20 
 21 WeightCase g_case[CASE_NUM];
 22 BOOL       g_isEvenCoin[MAX_COIN_NUM];
 23 char       g_coin[MAX_COIN_NUM];
 24 
 25 const char up[5]   = "up\0";
 26 const char down[5] = "down\0";
 27 const char even[5] = "even\0";
 28 
 29 void Input()
 30 {
 31     int i = 0;
 32     for(i = 0; i < CASE_NUM; i++)
 33     {
 34         scanf(" %s", g_case[i].str1);
 35         scanf(" %s", g_case[i].str2);
 36         scanf(" %s", g_case[i].delta);
 37         g_case[i].strLen = strlen(g_case[i].str1);
 38     }
 39 }
 40 
 41 void ProcEvenCase()
 42 {
 43     int i, j;
 44 
 45     for(i = 0; i < CASE_NUM; i++)
 46     {
 47         if(0 != strcmp(g_case[i].delta, even)) continue;
 48 
 49         for(j = 0; j < g_case[i].strLen; j++)
 50         {
 51             g_isEvenCoin[g_case[i].str1[j] - 'A'] = TRUE;
 52             g_isEvenCoin[g_case[i].str2[j] - 'A'] = TRUE;
 53         }
 54     }
 55 }
 56 
 57 void ProcOtherCase()
 58 {
 59     int i, j, delta;
 60 
 61     for(i = 0; i < CASE_NUM; i++)
 62     {
 63         if(0 == strcmp(g_case[i].delta, even)) continue;
 64 
 65         if(0 == strcmp(g_case[i].delta, up))
 66             delta = 1;
 67         else
 68             delta = -1;
 69 
 70         for(j = 0; j < g_case[i].strLen; j++)
 71         {
 72             if(!g_isEvenCoin[g_case[i].str1[j] - 'A']) g_coin[g_case[i].str1[j] - 'A'] += delta;
 73             if(!g_isEvenCoin[g_case[i].str2[j] - 'A']) g_coin[g_case[i].str2[j] - 'A'] -= delta;
 74         }
 75     }
 76 }
 77 
 78 void Proc()
 79 {
 80     memset(g_coin, 0, sizeof(g_coin));
 81     memset(g_isEvenCoin, FALSE, sizeof(g_isEvenCoin));
 82 
 83     ProcEvenCase();
 84     ProcOtherCase();
 85 }
 86 
 87 void Output()
 88 {
 89     int i, tmp, pos = 0, max = 0;
 90     char coin;
 91     for(i = 0; i < sizeof(g_coin); i++)
 92     {
 93         tmp = abs(g_coin[i]);
 94         if(tmp > max)
 95         {
 96             max = tmp;
 97             pos = i;
 98         }
 99     }
100     coin = pos+'A';
101 
102     if(g_coin[pos] < 0)
103         printf("%c is the counterfeit coin and it is light.\n", coin);
104     else
105         printf("%c is the counterfeit coin and it is heavy.\n", coin);
106 }
107 
108 int main()
109 {
110     int num = 0;
111     scanf("%d", &num);
112     while(num--)
113     {
114         Input();
115         Proc();
116         Output();
117     }
118 
119     return 0;
120 }

 

    原文作者:Online Judge POJ
    原文地址: https://www.cnblogs.com/bixiongquan/p/8890869.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞