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

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.

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

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

       需要注意的是當(dāng)假設(shè)一枚硬幣為假且通過三次稱量時,需要判斷三次稱量k的輕重情況是否一致,如果一次推得該硬幣較輕,而另一次卻判斷該硬幣較重,那么該硬幣肯定不是假幣。在判斷是需要注意當(dāng)左右兩邊都不出現(xiàn)假設(shè)為假的硬幣時,需要特殊處理,不能簡單的比較3次硬幣輕重是否相同,在左右兩邊都不出現(xiàn)該硬幣的情況下,不應(yīng)該把這次測量納入比較的范疇。除此之外需要的就是細(xì)心了,本人因為打印的時候多打印了個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];
        /*對當(dāng)前假設(shè)的硬幣進(jìn)行判斷*/
 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     }
        /*判斷結(jié)果是否一致*/
 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 }


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   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>
            欧美一区二视频| 国产精品国产a级| 欧美日韩免费一区| 久久精品国产亚洲一区二区三区 | 国产亚洲欧美一区二区三区| 欧美中文在线观看| 亚洲欧美变态国产另类| 在线午夜精品| 久久国产精品免费一区| 亚洲自拍都市欧美小说| 亚洲天堂偷拍| 午夜在线精品| 久久理论片午夜琪琪电影网| 久久精品91久久久久久再现| 久久综合久久久久88| 久久大综合网| 欧美理论在线播放| 国产精品福利片| 国内精品视频一区| 日韩午夜在线视频| 一本色道久久综合亚洲二区三区| 一道本一区二区| 久久免费黄色| 在线午夜精品| 欧美精品在线观看| 国内在线观看一区二区三区| 99国产精品久久久久老师| 亚洲欧美一级二级三级| 欧美1区视频| 午夜精品久久久久久久99热浪潮 | 亚洲欧美成人综合| 麻豆精品在线视频| 国产一区二区中文| 香蕉精品999视频一区二区| 亚洲人成网站999久久久综合| 亚洲美女在线看| 怡红院精品视频| 国产综合激情| 新狼窝色av性久久久久久| 亚洲区中文字幕| 欧美国产亚洲精品久久久8v| 今天的高清视频免费播放成人| 亚洲在线日韩| 亚洲欧美卡通另类91av| 国产精品国产三级国产专播品爱网| 亚洲经典在线看| 亚洲国产精品va在线看黑人| 免费在线观看日韩欧美| 亚洲级视频在线观看免费1级| 欧美国产日韩精品免费观看| 玖玖玖国产精品| 亚洲高清av| 日韩西西人体444www| 国产精品播放| 欧美黑人在线播放| 欧美色欧美亚洲高清在线视频| 在线亚洲高清视频| 亚洲欧美色婷婷| 在线视频国内自拍亚洲视频| 91久久极品少妇xxxxⅹ软件| 欧美日一区二区三区在线观看国产免| 在线亚洲一区二区| 久久久久免费观看| 中文欧美在线视频| 久久精品一本| 午夜精品久久久99热福利| 久久久久.com| 性色一区二区| 欧美日韩视频在线一区二区 | 一本大道久久a久久综合婷婷| 99re热这里只有精品免费视频| 国产精品高潮久久| 久久精品国产在热久久| 欧美视频中文在线看| 国产午夜精品久久| 久热这里只精品99re8久| 欧美视频一区二区在线观看| 鲁大师成人一区二区三区| 国产精品美女久久久浪潮软件| 亚洲春色另类小说| 亚洲第一搞黄网站| 久久精品国产亚洲5555| 久久久精品视频成人| 国产日韩欧美精品综合| 欧美一区二区黄| 久久噜噜噜精品国产亚洲综合| 国产精品有限公司| 午夜精品久久久久久久99水蜜桃| 亚洲专区在线视频| 国产欧美精品一区二区色综合 | 亚洲激情视频在线观看| 一区二区亚洲精品| 欧美成人免费大片| 99国产精品99久久久久久| 亚洲欧美一区二区三区极速播放| 国产精品视频一二三| 性做久久久久久久久| 欧美成人一区二区三区在线观看| 精品动漫3d一区二区三区| 免费成人av在线| 亚洲免费综合| 欧美大片网址| 欧美日韩国产一级| 久久成人一区| 亚洲亚洲精品三区日韩精品在线视频| 久久精品理论片| 一区二区三区 在线观看视| 国产农村妇女精品一二区| 欧美激情性爽国产精品17p| 午夜精品久久久久久久男人的天堂 | 男人插女人欧美| 国产精品看片资源| 狂野欧美激情性xxxx| 亚洲视频一区在线观看| 欧美国产欧美亚州国产日韩mv天天看完整| 日韩一区二区精品在线观看| 国产区精品视频| 国产精品久久久久久久久久ktv | 久久久激情视频| 亚洲永久精品国产| 一区二区三区 在线观看视| 91久久综合| 亚洲黄色成人网| 狠狠做深爱婷婷久久综合一区| 欧美日韩视频在线一区二区观看视频| 免费精品99久久国产综合精品| 久久精品成人| 久久久久成人精品免费播放动漫| 亚洲免费在线观看| 欧美一级电影久久| 久久久五月天| 欧美日韩另类国产亚洲欧美一级| 亚洲电影在线播放| 欧美日韩日日骚| 久久裸体视频| 亚洲欧美成人一区二区三区| 亚洲欧美清纯在线制服| 欧美freesex8一10精品| 亚洲电影一级黄| 久久久夜色精品亚洲| 亚洲视频精选| 在线综合欧美| 一区二区免费在线播放| 亚洲精品裸体| 亚洲精品精选| 麻豆精品视频在线| 久久精品视频在线| 欧美一区二区在线观看| 亚洲一区二区在线视频| 小黄鸭精品密入口导航| 国产精品婷婷午夜在线观看| 欧美一级欧美一级在线播放| 久久激情一区| 激情综合电影网| 亚洲国产精品专区久久| 99re8这里有精品热视频免费 | 久久成人免费| 欧美激情亚洲国产| 久久久久久久波多野高潮日日| 欧美日韩免费一区二区三区| 最新高清无码专区| 鲁大师影院一区二区三区| 欧美成人一二三| 激情综合自拍| 欧美国产三级| 欧美午夜精品一区二区三区| 久久aⅴ国产紧身牛仔裤| 国产在线欧美| 亚洲激情视频网站| 亚洲黄色成人网| 国产欧美日韩一区二区三区在线 | 久久不射网站| 韩国av一区二区| 欧美aa在线视频| 欧美性大战久久久久| 久久综合影视| 国产精品欧美日韩久久| 欧美成人精品在线播放| 国产精品久久久久久久久婷婷 | 日韩一级网站| 亚洲欧美日韩国产一区二区| 1024欧美极品| 久久精品最新地址| 亚洲一区中文字幕在线观看| 久久久亚洲欧洲日产国码αv| 亚洲一二三四久久| 欧美成人三级在线| 欧美91大片| 亚洲大片在线观看| 欧美一区二区三区精品电影| 亚洲小视频在线观看| 欧美波霸影院| 最新亚洲激情| 一区二区三区欧美在线| 欧美国产三区| 亚洲精品视频一区| 日韩视频中午一区| 欧美性jizz18性欧美| 亚洲视频在线二区| 欧美在线二区|