青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Counterfeit Dollar

該題ZOJ題號為1184, POJ題號為1013.

題目描述如下:

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.

【分析】該題屬于枚舉范疇。沒有比較巧妙的可以一步到位求出結果的方法,可以一次枚舉這12枚錢幣,假設其為假,然后代入到3次稱量判斷中,如果使三次判斷都成立且判斷結果相同,那么毫無疑問這枚錢幣是假的。首先可以進行預處理,比較結果為EVEN的可以判定兩邊的錢幣都是真的,不必參與到枚舉中來。對于上面的輸入用例,假設K是假的,代入判斷1,k不出現,那么兩邊重量應相等,成立。繼續稱量2,k出現在右邊,結果是UP,亦成立,且據此知道k是較輕的,因此k在右邊,而天平右邊翹起。下面進行判斷3

,k沒有出現在天平兩邊,而且結果為even成立。通過三次稱量判斷,且結果一致,可以肯定k就是假幣,且較輕。為了說明為題,對于上例假設L是假幣。代入稱量1L不出現,結果even成立,稱量2L不出現,結果為up不成立,因為只有一枚假幣,現假設L為假幣,而在L不出現的情況下天平不平衡,故L不是假幣。按照上述算法進行枚舉,遇到可以肯定是假幣的貨幣時算法終止。

       需要注意的是當假設一枚硬幣為假且通過三次稱量時,需要判斷三次稱量k的輕重情況是否一致,如果一次推得該硬幣較輕,而另一次卻判斷該硬幣較重,那么該硬幣肯定不是假幣。在判斷是需要注意當左右兩邊都不出現假設為假的硬幣時,需要特殊處理,不能簡單的比較3次硬幣輕重是否相同,在左右兩邊都不出現該硬幣的情況下,不應該把這次測量納入比較的范疇。除此之外需要的就是細心了,本人因為打印的時候多打印了個theWA6次,檢查了半個多小時,有種欲哭無淚的感覺。

具體代碼如下:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 char left[3][7], right[3][7];
  6 char result[3][6];
  7 int a[15];
  8 int w;
  9 
 10 int judge(char ch)
 11 {
 12     int r1, r2;
 13     int i;
 14     int a[3];
        /*對當前假設的硬幣進行判斷*/
 15     for (i = 0; i < 3++i)
 16     {
 17         r1 = strcmp(result[i], "even");
 18         r2 = strcmp(result[i], "up");
 19         if (strchr(left[i], ch) != NULL)
 20         {
 21             if (r1 == 0)
 22                 return 0;
 23             else if (r2  == 0)
 24                 a[i] = 1;
 25             else 
 26                 a[i] = -1;
 27         }
 28         else if (strchr(right[i], ch) != NULL)
 29         {
 30             if (r1 == 0)
 31                 return 0;
 32             else if (r2 == 0)
 33                 a[i] = -1;
 34             else 
 35                 a[i] = 1;
 36         }
 37         else
 38         {
 39             if (r1 != 0)
 40                 return 0;
 41             a[i] = 3;
 42         } 
 43     }
        /*判斷結果是否一致*/
 44     if (a[0!= 3)
 45         w = a[0];
 46     else if (a[1!= 3)
 47         w = a[1];
 48     else if (a[2!= 3)
 49         w = a[2];
 50     for (i = 0; i < 3++i)
 51     {
 52         if (a[i] != 3 && a[i] != w)
 53         {
 54                 return 0;
 55         }
 56     }
 57     return 1;
 58 }
 59 int main(void)
 60 {
 61     int n;
 62     int i;
 63     char *p;
 64     char ch;
 65     int r;
 66     scanf("%d%*c"&n);    
 67     while (n--)
 68     {
 69         memset(a, 0sizeof(a));
 70         for (i = 0; i < 3++i)
 71         {
 72             scanf("%s%s%s", left[i], right[i], result[i]);
 73             if (strcmp (result[i], "even"== 0)
 74             {
 75                 p = left[i];
 76                 while (*!= '\0')
 77                 {
 78                     a[*p-'A'= 1;
 79                     ++p;
 80                 }
 81                 p = right[i];
 82                 while (*!= '\0')
 83                 {
 84                     a[*p-'A'= 1;
 85                     ++p;
 86                 }
 87             }
 88         }
 89         for (ch = 'A'; ch <= 'L'++ch)
 90         {
 91             if (a[ch-'A']  == 1)
 92                 continue;
 93             r = judge(ch);
 94             if (r == 1)
 95             {
 96                 if (w > 0)
 97                 {
 98                     printf("%c is the counterfeit coin and it is heavy.\n", ch);
 99                 }
100                 else
101                 {
102                     printf("%c is the counterfeit coin and it is light.\n", ch);
103                 }
104                 break;
105             }
106         }
107     }
108     return 0;
109 }

posts - 12, comments - 1, trackbacks - 0, articles - 1

Copyright © 李東亮

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲国产精品电影| 亚洲欧美日韩中文在线制服| 亚洲国产精品va在线看黑人动漫| 久久久噜噜噜| 欧美日韩久久久久久| 亚洲伦理在线观看| 性欧美xxxx大乳国产app| 欧美激情第9页| 欧美在线3区| 亚洲靠逼com| 欧美日韩在线视频一区二区| 欧美亚洲综合网| 亚洲欧洲日本一区二区三区| 欧美肥婆在线| 午夜精品www| 99在线精品观看| 日韩视频中文| 亚洲国产高清在线| 久久精品欧美日韩| 欧美一区三区三区高中清蜜桃| 一区二区三区欧美激情| 国产精品99久久久久久久久| 亚洲精选一区| 亚洲专区一区| 新67194成人永久网站| 久久精品夜色噜噜亚洲a∨| 男同欧美伦乱| 欧美一区二区三区在线免费观看| 开心色5月久久精品| 国产精品成人一区二区| 亚洲国产综合在线| 欧美在线你懂的| 亚洲六月丁香色婷婷综合久久| 亚欧成人在线| 国产精品久久久久久久7电影| 激情欧美一区| 亚洲欧美日韩在线高清直播| 亚洲国产合集| 欧美成人在线免费观看| 1024亚洲| 欧美国产精品v| 蜜桃久久av| 亚洲电影在线播放| 免费不卡视频| 久久精品夜色噜噜亚洲a∨| 一区二区三区四区精品| 欧美国产日本在线| 国产亚洲精品久久久久久| 午夜国产欧美理论在线播放| 日韩一本二本av| 国产精品久久二区| 久久精品视频网| 久久青青草原一区二区| 亚洲国产人成综合网站| 亚洲精品视频在线观看网站| 欧美日韩一区二区三区在线观看免| 亚洲精品乱码久久久久久蜜桃麻豆 | 亚洲在线观看免费| 欧美日韩三级视频| 欧美一级理论性理论a| 久久久精品国产免费观看同学| 在线欧美福利| 亚洲一级黄色片| 欧美香蕉大胸在线视频观看| 欧美一二区视频| 能在线观看的日韩av| 午夜一区二区三区不卡视频| 美国成人直播| 久久人人看视频| 欧美午夜视频在线| 亚洲电影av| 国产日韩欧美a| 亚洲精品一品区二品区三品区| 国内精品久久久| 亚洲人成网站色ww在线| 欧美日韩中字| 亚洲品质自拍| 99国产麻豆精品| 女同性一区二区三区人了人一| 欧美专区在线观看一区| 欧美午夜精品一区| 一区二区三区四区精品| 亚洲欧美福利一区二区| 欧美视频精品一区| 日韩亚洲欧美精品| 亚洲永久视频| 国产一区深夜福利| 夜夜嗨av色综合久久久综合网| 亚洲福利视频一区| 欧美激情性爽国产精品17p| 亚洲第一精品在线| 99精品欧美一区二区蜜桃免费| 免费在线观看一区二区| 亚洲国产成人精品女人久久久| 99视频精品在线| 国产精品一区二区三区久久| 欧美一区二区三区四区夜夜大片 | 国产精品乱码人人做人人爱| 日韩视频精品在线| 性一交一乱一区二区洋洋av| 国产精一区二区三区| 久久九九全国免费精品观看| 亚洲国产清纯| 久久精品人人做人人爽| 亚洲免费av网站| 在线免费高清一区二区三区| 欧美午夜在线视频| 欧美精品网站| 蜜桃伊人久久| 亚洲女优在线| 亚洲国产第一| 一区二区三区在线视频播放| 欧美日一区二区三区在线观看国产免| 久久国产精品72免费观看| 亚洲美女在线观看| 亚洲国产精品一区二区第四页av| 久久激情网站| 久久久福利视频| 久久午夜羞羞影院免费观看| 先锋影音久久久| 香蕉久久夜色精品国产使用方法| 99精品国产福利在线观看免费| 91久久香蕉国产日韩欧美9色| 欧美视频日韩| 国产欧美日韩伦理| 国产亚洲精品自拍| 在线观看欧美日韩| 亚洲欧洲精品一区二区三区| 亚洲国产高清一区二区三区| 日韩视频亚洲视频| 亚洲欧美区自拍先锋| 久久精品国产久精国产一老狼| 欧美一区二区性| 牛牛影视久久网| 亚洲人体影院| 亚洲免费视频在线观看| 亚洲永久在线观看| 免费观看久久久4p| 欧美特黄一级大片| 国内精品亚洲| 亚洲影院高清在线| 亚洲盗摄视频| 欧美一区二区三区四区高清| 欧美精品激情| 一区二区三区在线视频免费观看| 亚洲精品在线一区二区| 久久亚洲影音av资源网| 亚洲午夜精品一区二区| 欧美精品一区二区三区蜜桃| 樱桃国产成人精品视频| 午夜精品区一区二区三| 久久久久久69| 国产一区二区黄| 亚洲欧美视频一区| 亚洲免费观看视频| 欧美电影在线观看完整版| 国产精品一区二区久久精品| 亚洲精品日韩在线| 麻豆成人在线观看| 久久久噜噜噜久久中文字免| 国产精品午夜av在线| 亚洲曰本av电影| 亚洲看片免费| 久久狠狠亚洲综合| 91久久午夜| 欧美极品一区| 洋洋av久久久久久久一区| 亚洲国产天堂久久国产91| 欧美激情网友自拍| 性欧美video另类hd性玩具| 欧美一区成人| 亚洲乱码一区二区| 亚洲私人影院| 亚洲福利在线看| 亚洲视频日本| 在线免费不卡视频| 亚洲无限av看| 亚洲日本电影| 久久免费99精品久久久久久| 在线观看精品| 在线视频你懂得一区| 黄色精品免费| 99在线热播精品免费| 国产午夜精品一区二区三区欧美| 久久免费偷拍视频| 欧美喷水视频| 久久亚洲捆绑美女| 国产精品久久久久久久久久免费 | 国语精品一区| 亚洲精品偷拍| 狠狠色伊人亚洲综合网站色| 91久久国产精品91久久性色| 国产精品福利影院| 亚洲福利视频在线| 亚洲国产精品综合| 久久久久一区二区三区| 久久精品日韩| 在线电影一区| 欧美.com| 亚洲精品在线三区|