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

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>
            日韩天堂av| 亚洲婷婷在线| 猛男gaygay欧美视频| 香蕉乱码成人久久天堂爱免费 | 欧美在线观看一区二区三区| 一本久久青青| 国产精品网站在线| 久久狠狠亚洲综合| 香蕉视频成人在线观看| 亚洲男女毛片无遮挡| 国产一区91精品张津瑜| 久久全国免费视频| 欧美不卡一卡二卡免费版| 日韩亚洲欧美一区二区三区| 亚洲视频播放| 一区二区亚洲精品国产| 在线亚洲电影| 午夜精品一区二区三区四区 | 欧美日韩无遮挡| 亚洲欧美国产日韩中文字幕| 欧美一区二区三区视频免费| 1000部国产精品成人观看| 亚洲精品日日夜夜| 国产亚洲欧美激情| 亚洲丰满在线| 国产精品色午夜在线观看| 噜噜爱69成人精品| 欧美日韩另类一区| 久久只有精品| 国产精品v欧美精品v日韩 | 性欧美办公室18xxxxhd| 欧美在线视频导航| 99精品国产在热久久下载| 亚洲一区日本| 亚洲作爱视频| 久久精品三级| 国产一区二区精品久久| 91久久久久久久久| 国内精品伊人久久久久av影院| 亚洲国产视频一区| 国产日韩精品视频一区二区三区| 亚洲大胆av| 国产一区二区成人| 99精品欧美一区二区三区| 永久91嫩草亚洲精品人人| 亚洲一区bb| 日韩一级成人av| 久久米奇亚洲| 久久视频国产精品免费视频在线| 欧美涩涩网站| 亚洲精品国产精品国自产在线 | 欧美成人午夜激情视频| 久久精品国产欧美亚洲人人爽| 欧美日韩大片一区二区三区| 母乳一区在线观看| 国内激情久久| 性色av一区二区三区红粉影视| 中文无字幕一区二区三区| 欧美高清视频| 亚洲大片精品永久免费| 激情国产一区| 久久国产日韩| 久久美女性网| 狠狠88综合久久久久综合网| 性欧美1819性猛交| 久久国产视频网站| 国产午夜精品理论片a级大结局| 一区二区三区免费在线观看| 亚洲视频在线视频| 欧美日韩在线视频一区| 日韩亚洲欧美高清| 亚洲视频免费看| 欧美性淫爽ww久久久久无| 中文精品视频| 亚洲欧美制服中文字幕| 国产精品日韩欧美一区二区| 亚洲综合大片69999| 久久精品国产亚洲精品| 国产一区免费视频| 久久婷婷综合激情| 欧美激情bt| 一区二区电影免费观看| 欧美午夜寂寞影院| 亚洲一区bb| 久久综合久色欧美综合狠狠| 伊人色综合久久天天| 男女激情久久| 一区二区三区产品免费精品久久75| 亚洲一级黄色| 国产在线观看精品一区二区三区| 久久久亚洲高清| 午夜免费日韩视频| 国产主播一区二区三区四区| 乱中年女人伦av一区二区| 亚洲国产精品一区| 亚洲免费在线播放| 国产一区二区三区四区三区四 | 亚洲视频欧美在线| 久久久蜜桃一区二区人| 最新国产拍偷乱拍精品 | 久久久综合网站| 亚洲激情影视| 久久国产99| 亚洲精品一二区| 国产精品永久免费在线| 老牛嫩草一区二区三区日本| 日韩一级精品视频在线观看| 久久噜噜噜精品国产亚洲综合| 在线视频国产日韩| 国产精品白丝av嫩草影院| 久久精品久久综合| 日韩视频免费| 欧美国产日韩一区| 久久xxxx| 亚洲午夜成aⅴ人片| 伊人夜夜躁av伊人久久| 欧美先锋影音| 欧美大片一区二区三区| 午夜精品国产| 在线视频亚洲| 亚洲黄色一区二区三区| 久久青草福利网站| 亚洲欧洲av一区二区三区久久| 91久久线看在观草草青青| 国产女主播一区二区三区| 欧美精品日本| 美女黄毛**国产精品啪啪| 欧美中文在线字幕| 亚洲一区二区黄| aa亚洲婷婷| 亚洲日本无吗高清不卡| 欧美成人精品一区| 久久影视三级福利片| 久久av二区| 性色av香蕉一区二区| 中文在线不卡视频| 亚洲乱码国产乱码精品精98午夜 | 精品不卡在线| 国产亚洲欧美另类中文| 国产毛片一区| 国产欧美精品一区二区三区介绍| 欧美色欧美亚洲另类二区| 欧美顶级艳妇交换群宴| 蜜臀久久久99精品久久久久久| 久久精品国产成人| 欧美一级电影久久| 羞羞答答国产精品www一本| 亚洲视频精选| 亚洲欧美中日韩| 午夜欧美大尺度福利影院在线看 | 亚洲激情成人网| 亚洲黄色一区| 日韩写真在线| 亚洲午夜精品在线| 亚洲欧美精品| 久久av最新网址| 久久久蜜臀国产一区二区| 久久综合给合| 欧美刺激午夜性久久久久久久| 欧美金8天国| 国产精品成人免费精品自在线观看| 欧美日韩精品国产| 国产精品久久久| 国产一区二区三区久久精品| 红杏aⅴ成人免费视频| 亚洲黑丝在线| 亚洲视频欧美视频| 欧美一区2区三区4区公司二百| 久久精品夜色噜噜亚洲a∨| 久久夜色精品国产欧美乱极品| 欧美1区2区| 夜夜嗨av一区二区三区中文字幕| 亚洲一级片在线看| 久久精品一区二区三区四区| 免费在线看成人av| 国产精品欧美精品| 激情五月综合色婷婷一区二区| 亚洲久色影视| 久久久久久久综合色一本| 亚洲成人资源网| 亚洲欧美久久久| 浪潮色综合久久天堂| 国产精品a久久久久久| 一区二区三区在线视频观看| 日韩视频在线一区| 久久久久国产精品麻豆ai换脸| 亚洲国产精品黑人久久久| 亚洲砖区区免费| 欧美www在线| 国产一区欧美| 亚洲香蕉网站| 欧美激情一区| 欧美一区1区三区3区公司| 欧美激情一区二区三级高清视频 | 欧美激情1区2区3区| 国产精品永久免费视频| 日韩一二三区视频| 浪潮色综合久久天堂| 亚洲午夜精品久久久久久app| 老牛嫩草一区二区三区日本|