zoukankan      html  css  js  c++  java
  • poj1013.Counterfeit Dollar(枚举)

    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] != '' ; s ++ ) {
    55                     l += a[e[k].l[s] - 'A'] ;
    56                 }
    57                 for (int s = 0 ; e[k].r[s] != '' ; 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] != '' ; s ++ ) {
    80                     l += a[e[k].l[s] - 'A'] ;
    81                 }
    82                 for (int s = 0 ; e[k].r[s] != '' ; 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.
    " , A , b ) ;
    96     }
    97     return 0 ;
    98 }
    View Code
  • 相关阅读:
    ssh 面试
    通达信实战主力阵地副图指标公式
    巧用主力进出、主力买卖指标进行波段操作——逃顶和抄底
    散户线真的好用
    一位良心发现的交易员自述:我们是怎么玩弄散户的
    Xamrin开发安卓笔记(三)
    Xamrin开发安卓笔记(二)
    Xamrin开发安卓笔记(一)
    【教程】Microsoft Visual Studio 2015 安装Android SDK
    VS2015配置安卓Android和iOS开发环境
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4360297.html
Copyright © 2011-2022 走看看