北大poj- 1013

Counterfeit Dollar

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

Descriptionapp

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.

Inputide

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.

Outputui

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 Inputspa

1 
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 

Sample Outputcode

K is the counterfeit coin and it is light. 

Sourceorm

 
分析:
頗有意思的題目,人腦很好處理,可是用計算機來作就有點不知如何下手了。
拿到題目,列出方程,各類約,就能獲得答案,可是這種方法,用計算機很差實現。後來忽然想到,能夠用「嫌疑人」的方法來作。
解題:
已知條件:一、只有一枚假幣;二、假幣可重可輕;三、必定能夠根據已知的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 }
相關文章
相關標籤/搜索