首页 > 其他 > 详细

poj1013.Counterfeit Dollar(枚举)

时间:2015-03-23 17:23:45      阅读:266      评论:0      收藏:0      [点我收藏+]

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 
 3 int main()
 4 {
 5     //freopen ("a.txt" , "r" , stdin ) ;
 6     int people[50] = {0}, k, Joseph[14] = {0};//Joseph用于打表,不然超时
 7     while(scanf("%d", &k) != EOF && k != 0)
 8     {
 9         if (Joseph[k] != 0)
10         {
11             printf("%d\n", Joseph[k]);
12             continue;
13         }
14         int n = 2 * k;
15         int m = 1;
16         people[0] = 0;//people[0] = 0表示编号从0开始
17         for (int i = 1; i <= k; i++)
18         {
19             //每次枚举完毕将剩下的人按从0到n - i编号,只要好人没有杀掉,则前面k - 1个编号是不变的
20             people[i] = (people[i - 1] + m - 1) % (n - i + 1);
21           //  printf ("[%d] = %d , " , i , people[i] ) ;
22             if (people[i] < k)//第k个人的编号为k - 1,所以这里是<而不是<=
23             {
24                 i = 0 ;
25                 m++;
26             }
27         }
28        // printf ("\n") ;
29         Joseph[k] = m;
30         printf("%d\n", m);
31     }
32     return 0;
33 }
View Code

 

poj1013.Counterfeit Dollar(枚举)

原文:http://www.cnblogs.com/get-an-AC-everyday/p/4360297.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!