Counterfeit Dollar
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36206   Accepted: 11561

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. 1
  2. ABCD EFGH even
  3. ABCI EFJK up
  4. ABIJ EFGH even

Sample Output

  1. K is the counterfeit coin and it is light.
    题目大意:有12枚硬币,里面有一个是假的,称三次,判断哪枚是假的,并且判断假的比真的轻还是重。
    解题方法:直接枚举,总共有24中情况,即每一枚硬币都有可能比真的轻或者重,枚举这24种情况,即可得出答案。
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. char str1[], str2[], balance[];
  7. int nCase, ans[];//ans用于记录每种情况满足的次数有多少次
  8. scanf("%d", &nCase);
  9. while(nCase--)
  10. {
  11. memset(ans, , sizeof(ans));
  12. for (int i = ; i < ; i++)
  13. {
  14. scanf("%s%s%s", str1, str2, balance);
  15. //j等于0到11依次表示A-L中假币轻,j等于12到23依次表示A-L中假币重
  16. for (int j = ; j < ; j++)
  17. {
  18. switch(balance[])
  19. {
  20. case 'e':
  21. {
  22. bool flag = true;
  23. //如果假币轻则在两边都应该找不到该硬币
  24. if (j < )
  25. {
  26. for (int k = ; k < strlen(str1); k++)
  27. {
  28. if (str1[k] == 'A' + j)
  29. {
  30. flag = false;
  31. }
  32. }
  33. for (int k = ; k < strlen(str2); k++)
  34. {
  35. if (str2[k] == 'A' + j)
  36. {
  37. flag = false;
  38. }
  39. }
  40. }
  41. //如果假币重则在两边都应该找不到该硬币
  42. else
  43. {
  44. for (int k = ; k < strlen(str1); k++)
  45. {
  46. if (str1[k] == 'A' + j - )
  47. {
  48. flag = false;
  49. }
  50. }
  51. for (int k = ; k < strlen(str2); k++)
  52. {
  53. if (str2[k] == 'A' + j - )
  54. {
  55. flag = false;
  56. }
  57. }
  58. }
  59. //如果两边都没找到,则说明满足条件
  60. if (flag)
  61. {
  62. ans[j]++;
  63. }
  64. }
  65. break;
  66. case 'u':
  67. {
  68. bool flag = false;
  69. //如果左边重则轻的假币放在右边
  70. if (j < )
  71. {
  72. for (int k = ; k < strlen(str2); k++)
  73. {
  74. if (str2[k] == 'A' + j)
  75. {
  76. flag = true;
  77. }
  78. }
  79. }
  80. //如果左边重则重的假币放在左边
  81. else
  82. {
  83. for (int k = ; k < strlen(str1); k++)
  84. {
  85. if (str1[k] == 'A' + j - )
  86. {
  87. flag = true;
  88. }
  89. }
  90. }
  91. if (flag)
  92. {
  93. ans[j]++;
  94. }
  95. }
  96. break;
  97. case 'd':
  98. {
  99. bool flag = false;
  100. //如果右边重,则轻的假币放在左边
  101. if (j < )
  102. {
  103. for (int k = ; k < strlen(str1); k++)
  104. {
  105. if (str1[k] == 'A' + j)
  106. {
  107. flag = true;
  108. }
  109. }
  110. }
  111. //如果右边重,则重的假币放在右边
  112. else
  113. {
  114. for (int k = ; k < strlen(str2); k++)
  115. {
  116. if (str2[k] == 'A' + j - )
  117. {
  118. flag = true;
  119. }
  120. }
  121. }
  122. if (flag)
  123. {
  124. ans[j]++;
  125. }
  126. }
  127. break;
  128. }
  129. }
  130. }
  131. for (int i = ; i < ; i++)
  132. {
  133. //ans[i] == 3说明这种情况满足三次称量的结果
  134. if (ans[i] == )
  135. {
  136. //假币比真币轻
  137. if (i < )
  138. {
  139. printf("%c is the counterfeit coin and it is light.\n", i + 'A');
  140. }
  141. //假币比真币重
  142. else
  143. {
  144. printf("%c is the counterfeit coin and it is heavy.\n", i - + 'A');
  145. }
  146. break;
  147. }
  148. }
  149. }
  150. return ;
  151. }

POJ 1013 Counterfeit Dollar的更多相关文章

  1. Poj 1013 Counterfeit Dollar / OpenJudge 1013(2692) 假币问题

    1.链接地址: http://poj.org/problem?id=1013 http://bailian.openjudge.cn/practice/2692 http://bailian.open ...

  2. POJ 1013 Counterfeit Dollar 集合上的位运算

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

  3. 思维+模拟--POJ 1013 Counterfeit Dollar

    Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver d ...

  4. POJ 1013:Counterfeit Dollar

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42028   Accepted: 13 ...

  5. Counterfeit Dollar 分类: POJ 2015-06-12 15:28 19人阅读 评论(0) 收藏

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41559   Accepted: 13 ...

  6. POJ1013 Counterfeit Dollar

    题目来源:http://poj.org/problem?id=1013 题目大意:有12枚硬币,其中有一枚假币.所有钱币的外表都一样,所有真币的重量都一样,假币的重量与真币不同,但我们不知道假币的重量 ...

  7. Counterfeit Dollar -----判断12枚钱币中的一个假币

     Counterfeit Dollar Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  8. poj1013.Counterfeit Dollar(枚举)

    Counterfeit Dollar Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 415  Solved: 237 Description Sally ...

  9. POJ 1013 小水题 暴力模拟

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35774   Accepted: 11 ...

随机推荐

  1. [自制简单操作系统] 7、多任务(二)——任务管理自动化&任务休眠

    前言 >_<" 这里仿照窗口管理的方式将任务管理也修改成相应的管理模式,这样可以灵活的添加多个任务,而不必每次都要修改任务切换函数:此外还在任务休眠做了尝试,通过将任务挂起和唤醒 ...

  2. Linux:软件安装

    Linux 上的软件安装 四种安装方式 在线安装 从磁盘安装盘deb软件包 从二进制软件包安装 从源代码编译安装 在线安装 在不同的linux发行版上面在线安装方式会有一些差异包括使用的命令及它们的包 ...

  3. 安装 Dubbo 管理控制台

    Dubbo管控台可以对注册到 zookeeper 注册中心的服务或服务消费者进行管理,但管控台是否正常对Dubbo服务没有影响,管控台也不需要高可用,因此可以单节点部署. IP: 192.168.1. ...

  4. atitit.hbnt orm db 新新增更新最佳实践o99

    atitit.hbnt orm db 新新增更新最佳实践o99 1. merge跟个save了. 1 2. POJO对象处于游离态.持久态.托管态.使用merge()的情况. 1 3. @Dynami ...

  5. Spark Scala 读取GBK文件的方法

    1. 在生产环境下,很多文件是GBK编码格式的,而SPARK 常用的textFile方法默认是写死了读UTF-8格式的文件,其他格式文件会显示乱码 用如下代码实现读取GBK文件的方法 import o ...

  6. iOS开发——高级技术&调用地图功能的实现

    调用地图功能的实现 一:苹果自带地图 学习如逆水行舟,不进则退.古人告诉我们要不断的反思和总结,日思则日精,月思则月精,年思则年精.只有不断的尝试和总结,才能让我们的工作和生活更加 轻松愉快和美好.连 ...

  7. 利用jsoup爬虫工具,爬取数据,并利用excel导出

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; i ...

  8. python redis使用

    #!/usr/bin/python #coding=utf-8 import redis class CRedis: def __init__(self): self.host = 'localhos ...

  9. 开发ERP软件应该遵守的22条规则

    总结一下做管理软件,有哪些项是经过检验的条款,必须遵守的. 界面篇 1  要保存用户的偏号(profile/favourite). ASP.NET 2.0引入此功能,当用户修改默认的控件的属性时,框架 ...

  10. go语言实现一个简单的登录注册web小程序

    最近学习golang也有一段时间了,基础差不多学了个大概,因为本人是java程序员,所以对web更感兴趣.根据<go web编程>中的例子改编一个更简单的例子,供新手参考,废话不多说,上菜 ...