poj1013.Counterfeit Dollar(枚举)

Sally Jones 在拥有一打银币的情况下,只有十一只是真的银币,而另一只是伪造品,且其重量不同但无法通过颜色和大小区分。她利用准确的平衡秤进行三次称重来找出伪造银币,无论其是较轻还是较重。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Counterfeit Dollar

Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 415  Solved: 237

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. 

HINT

一开始想的死复杂,后来同学跟我说只要assume一个字母为假 , 并分别assume它为正或负,枚举即可。

想想也是 ,才12个。orz

 1 #include<stdio.h>
 2 #include<string.h>
 3 int T ;
 4 bool a[20] ;
 5 char b[20] , A ;
 6 bool flag , blog;
 7  
 8 struct st
 9 {
10     char l[20] , r[20] ;
11     char jud[20] ;
12 }e[20];
13  
14 void solve (int k , int l , int r)
15 {
16     if (l == r ) {
17         if (strcmp (e[k].jud , "even") == 0) {
18             return ;
19         }
20     }
21     else if (l > r) {
22         if (strcmp (e[k].jud , "up") == 0 ) {
23             return ;
24         }
25     }
26     else if (l < r) {
27         if (strcmp (e[k].jud , "down") == 0 ) {
28             return ;
29         }
30     }
31     blog = 0 ;
32 }
33  
34  
35 int main ()
36 {
37    // freopen ("a.txt" , "r" , stdin ) ;
38     scanf ("%d" , &T ) ;
39     while (T--) {
40         for (int i = 1 ; i <= 3 ; i++) {
41             scanf ("%s" , e[i].l) ;// puts (e[i].l) ;
42             scanf ("%s" , e[i].r) ;// puts (e[i].r) ;
43             scanf ("%s" , e[i].jud) ;// puts (e[i].jud) ;
44         }
45         flag = 0 ;
46         for (int i = 0 ; i < 12 && !flag ; i++) {
47             for (int j = 0 ; j < 12 ; j++) {//假的比较轻
48                 a[j] = 1 ;
49             }
50             a[i] = 0 ;
51             blog = 1 ;
52             for (int k = 1 ; k <= 3 ; k++) {
53                 int l = 0 , r = 0 ;
54                 for (int s = 0 ; e[k].l[s] != '\0' ; s ++ ) {
55                     l += a[e[k].l[s] - 'A'] ;
56                 }
57                 for (int s = 0 ; e[k].r[s] != '\0' ; s ++ ) {
58                     r += a[e[k].r[s] - 'A'] ;
59                 }
60                 solve (k , l , r) ;
61                 if (!blog)
62                     break ;
63             }
64             if (blog) {
65                 flag = 1 ;
66                 A = 'A' + i  ;
67                 strcpy (b , "light") ;
68             }
69         }
70         if (!flag)
71         for (int i = 0 ; i < 12 && !flag ; i++) {
72             for (int j = 0 ; j < 12 ; j++) {//假的比较重
73                 a[j] = 0 ;
74             }
75             a[i] = 1 ;
76             blog = 1 ;
77             for (int k = 1 ; k <= 3 ; k++) {
78                 int l = 0 , r = 0 ;
79                 for (int s = 0 ; e[k].l[s] != '\0' ; s ++ ) {
80                     l += a[e[k].l[s] - 'A'] ;
81                 }
82                 for (int s = 0 ; e[k].r[s] != '\0' ; s ++ ) {
83                     r += a[e[k].r[s] - 'A'] ;
84                 }
85                 solve (k , l , r) ;
86                 if (!blog)
87                     break ;
88             }
89             if (blog) {
90                 flag = 1 ;
91                 A = 'A' + i  ;
92                 strcpy (b , "heavy" ) ;
93             }
94         }
95         printf ("%c is the counterfeit coin and it is %s.\n" , A , b ) ;
96     }
97     return 0 ;
98 }
View Code

 

转载于:https://www.cnblogs.com/get-an-AC-everyday/p/4360297.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值