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

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是假的,代入判斷1k不出現,那么兩邊重量應相等,成立。繼續稱量2k出現在右邊,結果是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 }


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


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>
            欧美中在线观看| 久久午夜色播影院免费高清| 欧美另类一区| aaa亚洲精品一二三区| 亚洲精品自在久久| 欧美午夜不卡在线观看免费| 亚洲一区日韩在线| 亚洲影院高清在线| 黑人巨大精品欧美一区二区小视频| 久久九九久久九九| 巨胸喷奶水www久久久免费动漫| 91久久久久久国产精品| 亚洲人成亚洲人成在线观看图片| 欧美乱大交xxxxx| 欧美伊久线香蕉线新在线| 久久精品水蜜桃av综合天堂| 亚洲激情影院| 亚洲图片你懂的| 影音先锋亚洲精品| 亚洲乱码国产乱码精品精可以看| 国产精品视频免费一区| 免费看的黄色欧美网站| 欧美sm视频| 欧美久久久久久蜜桃| 欧美一区二区三区四区夜夜大片 | 欧美有码在线观看视频| 久久精品一区二区| 在线一区亚洲| 久久久久久久综合色一本| 日韩视频在线观看| 欧美中文字幕在线| 中日韩午夜理伦电影免费| 欧美在线高清| 亚洲一区二区网站| 蜜桃av噜噜一区| 久久久久国产精品人| 欧美人牲a欧美精品| 久久夜色精品国产噜噜av| 欧美视频一区二区三区| 免费观看30秒视频久久| 国产欧美精品一区二区三区介绍| 亚洲国产cao| 伊人久久大香线| 午夜精品福利视频| 亚洲一区黄色| 欧美理论电影网| 亚洲第一中文字幕| 黄色一区二区在线观看| 亚洲在线网站| 亚洲在线成人| 欧美日韩国产一区二区三区地区 | 日韩视频在线观看免费| 欧美在线视频免费观看| 午夜精品久久久久影视| 欧美日一区二区三区在线观看国产免| 欧美不卡高清| 亚洲第一黄色| 久久久久国产精品午夜一区| 久久久精品五月天| 国产日韩视频| 亚洲欧美在线磁力| 欧美在线免费看| 国产精品天天看| 亚洲淫性视频| 久久精品日产第一区二区三区| 国产精品萝li| 亚洲一区www| 欧美一区二区大片| 国产乱码精品一区二区三区不卡| 亚洲一区免费视频| 欧美一区二区成人| 国产一区日韩一区| 久久经典综合| 欧美激情第三页| 亚洲精品在线观看视频| 欧美激情性爽国产精品17p| 亚洲国产你懂的| 亚洲网站在线看| 国产精品久久久久久妇女6080| 亚洲视频福利| 久久亚洲美女| 亚洲免费电影在线| 国产精品嫩草久久久久| 欧美一级视频一区二区| 老牛国产精品一区的观看方式| 亚洲国产91| 欧美三级第一页| 性亚洲最疯狂xxxx高清| 欧美 日韩 国产在线| 99精品福利视频| 国产美女精品免费电影| 久久久久国产精品www| 亚洲福利视频三区| 亚洲欧美日韩中文在线制服| 国产在线观看精品一区二区三区 | 亚洲欧美日韩精品久久久| 久久久精品一区| 亚洲美女色禁图| 国产精品日韩欧美一区| 久久久久久999| 999亚洲国产精| 母乳一区在线观看| 亚洲午夜精品一区二区三区他趣| 国产亚洲欧美日韩日本| 欧美精品三区| 久久久精品日韩欧美| 一本色道**综合亚洲精品蜜桃冫| 久久久久久一区二区| 一区二区三区日韩欧美| 激情综合五月天| 欧美亚男人的天堂| 免费日韩视频| 久久精品噜噜噜成人av农村| 99国产精品久久久久久久久久 | 亚洲电影成人| 久久精品国产久精国产一老狼| 日韩视频免费在线| 狠狠干狠狠久久| 国产欧美日韩在线观看| 欧美精品成人一区二区在线观看| 欧美一区二区三区免费观看| 99精品99| 亚洲激情一区二区三区| 免费欧美日韩| 久久久久久国产精品mv| 欧美一区二区女人| 亚洲欧美日韩一区在线观看| 亚洲最新视频在线| 亚洲精品在线二区| 亚洲国产精品一区二区www| 国产一区二区三区久久悠悠色av | 亚洲影视九九影院在线观看| 亚洲精品久久久久久久久| 在线不卡a资源高清| 国产一区二区三区四区| 国产欧美日韩精品一区| 国产精品视频xxx| 国产精品老牛| 国产精品久久久久久模特| 欧美午夜大胆人体| 国产精品成人一区二区网站软件| 欧美日韩视频在线一区二区观看视频 | 久久综合色天天久久综合图片| 欧美中文字幕在线| 欧美一区二区久久久| 先锋影音一区二区三区| 欧美一区二区视频在线观看| 欧美一区二区私人影院日本| 欧美一区二区日韩一区二区| 性欧美xxxx大乳国产app| 性做久久久久久免费观看欧美| 亚洲欧美日韩久久精品| 性欧美长视频| 久久夜色精品国产亚洲aⅴ| 久久这里有精品视频| 欧美福利影院| 亚洲精品美女久久久久| 在线视频欧美日韩| 欧美一区二区播放| 久久久久久穴| 欧美连裤袜在线视频| 国产精品草草| 国产亚洲一级高清| 亚洲国产经典视频| 一区二区三区国产精品| 欧美亚洲一区二区在线观看| 久久久激情视频| 亚洲福利视频一区二区| 夜夜嗨av一区二区三区| 欧美亚洲综合在线| 欧美高清视频www夜色资源网| 欧美日韩1区2区3区| 国产欧美精品日韩区二区麻豆天美| 激情欧美一区| 亚洲丝袜av一区| 久久一区国产| 一区二区高清视频在线观看| 小黄鸭视频精品导航| 欧美成人精品在线| 国产精品家庭影院| 亚洲国产精品va在线看黑人| 亚洲先锋成人| 欧美成人免费小视频| 亚洲一区二区三区精品在线| 久久手机精品视频| 欧美视频一区二区三区…| 伊人精品成人久久综合软件| 亚洲综合色丁香婷婷六月图片| 久久久久久网站| 亚洲精品一线二线三线无人区| 欧美一区二区三区免费视频| 欧美日韩国产精品| …久久精品99久久香蕉国产| 欧美一区二区三区久久精品茉莉花 | 久久婷婷一区| 亚洲天堂久久| 欧美日韩1080p| 在线精品在线| 久久久人人人| 午夜欧美大片免费观看|