Counterfeit Dollar
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 52474   Accepted: 16402

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.

Source

大概意思就是有12枚硬币从A到L,其中有一枚是假币,但是不知道轻重。称了三次,每次给定左边硬币和右边硬币以及右边天秤的情况,三次称量一定会确定一枚假币,问哪个硬币是假的并说明假币轻了还是重了。
 
思路:把十二枚硬币的情况都枚举一遍,一枚硬币如果是假币会有是轻假币还是重假币的情况,都需要枚举出来。从A到L依次假设为假币,看是否符合三次称量,因为只有一个假币,所以肯定只有一个硬币是假币时,才会满足给出的三次测量的情形。
  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. char cleft[3][7];//一共12个硬币,每次一边最多6个
  5. char cright[3][7];
  6. char result[3][7];
  7. int num;
  8. bool isFake(char c,bool heavy);//不加括号体
  9. int main()
  10. {
  11.  
  12. cin >> num;
  13. while(num-- !=0)
  14. {
  15. for(int i = 0;i<3;i++)
  16. {
  17. cin >> cleft[i] >>cright[i]>>result[i];
  18. }
  19. for(char i = 'A';i<='L';++i)
  20. {
  21. if(isFake(i,true))
  22. {
  23. cout <<i<<" is the counterfeit coin and it is heavy. "<<endl;
  24. break;
  25. }
  26. else if(isFake(i,false))
  27. {
  28. cout <<i<<" is the counterfeit coin and it is light. "<<endl;
  29. break;
  30. }
  31. }
  32. }
  33. return 0;
  34. }
  35.  
  36. bool isFake(char c,bool heavy)
  37. {
  38. for(int i = 0;i<3;++i)
  39. {
  40. switch(result[i][0])
  41. {
  42. case 'u':
  43. if(heavy == true && strchr(cleft[i],c)==NULL )//重 假币肯定在左边
  44. return false;
  45. if(heavy == false && strchr(cright[i],c)==NULL )//轻 假币肯定在右边
  46. return false;
  47. break;
  48. case 'd':
  49. if(heavy == true && strchr(cright[i],c)==NULL )//假币肯定在右边,并且重
  50. return false;
  51. if(heavy == false && strchr(cleft[i],c)==NULL )//假币肯定在左边,并且轻
  52. return false;
  53. break;
  54. case 'e':
  55. if(strchr(cleft[i],c)!=NULL || strchr(cright[i],c)!=NULL )//假币不在两边
  56. return false;
  57. break;
  58. }
  59. }
  60. return true;
  61. }

strchr语法:
  #include <string.h>
  char *strchr( const char *str, int ch );
功能:函数返回一个指向str 中ch 首次出现的位置,当没有在str 中找ch到返回NULL。

POJ1013称硬币【枚举】的更多相关文章

  1. POJ1013 称硬币

    题目链接:http://poj.org/problem?id=1013 题目大意 有12枚硬币.其中有11枚真币和1枚假币.假币和真币重量不同,但不知道假币比真币轻还是重.现在,用一架天平称了这些币三 ...

  2. 枚举-称硬币POJ1013

    #include <iostream> #include<string.h> using namespace std; char Lleft[][]; char Lright[ ...

  3. 算法题----称硬币: 2n(并不要求n是2的幂次方)个硬币,有两个硬币重量为m+1, m-1, 其余都是m 分治 O(lgn)找出假币

    Description: 有2n个硬币和一个天平,其中有一个质量是m+1, 另一个硬币质量为m-1, 其余的硬币质量都是m. 要求:O(lgn)时间找出两枚假币 注意: n不一定是2的幂次方 算法1: ...

  4. 洛谷P1441 砝码称重 枚举 + 01背包

    显然,n<=20, m<=4 的数据范围一眼爆搜. 直接搜索一下不用哪4个砝码,再做一遍01背包即可. 可能是本人太菜鸡,01背包部分调了半天QAQ-- #include<cstdi ...

  5. C++基础算法学习——猜假币

    有12枚硬币.其中有11枚真币和1枚假币.假币和真币重量不同,但不知道假币比真币轻还是重.现在,用一架天平称了这些币三次,告诉你称的结果,请你找出假币并且确定假币是轻是重(数据保证一定能找出来).例题 ...

  6. 2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)

    心得: 这比赛真的是不要不要的,pending了一下午,也不知道对错,直接做过去就是了,也没有管太多! Problem A: 两只老虎 Description 来,我们先来放松下,听听儿歌,一起“唱” ...

  7. IOS 枚举 enum

    前言:oc中枚举的正确使用,可以增强代码的可读性,减少各种“错误”,让代码更加的规范.下面先介绍枚举的用法,最后介绍个人对枚举的理解,什么是枚举,为什么用枚举. 一. OC中,枚举的使用 1. 写法1 ...

  8. (BruteForce)暴力破解经典题目总结

    在算法竞赛中,很多问题是来不及用数学公式推导出来的.或者说根本就找不到数学规律,这时我们就需要使用枚举来暴力破解. 不过枚举也是需要脑子的,一味的暴力只能超时.因此我这里选择了几道mooc上经典的题目 ...

  9. ACM训练计划建议(写给本校acmer,欢迎围观和指正)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

随机推荐

  1. jira以及jira API简单介绍

    最近需要预言:是否可以通过jira API实现用例管理,对jira的应用.API.扩展等进行了一定的了解. Jira介绍: jira是目前比较流行的基于Java架构的管理系统(Atlassian公司支 ...

  2. nginx 配置tp3.2

    server { listen 80; server_name 域名; #charset koi8-r; #access_log /var/log/nginx/host.access.log main ...

  3. Hdu 3289 Rain on your Parade (二分图匹配 Hopcroft-Karp)

    题目链接: Hdu 3289 Rain on your Parade 题目描述: 有n个客人,m把雨伞,在t秒之后将会下雨,给出每个客人的坐标和每秒行走的距离,以及雨伞的位置,问t秒后最多有几个客人可 ...

  4. POJ 2002 Squares 数学 + 必须hash

    http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...

  5. c# 搜狗拼音输入法,刷输入速度和累计输入

    事件起因: 搜狗拼音有几个称号(光速超人:要求最快打字速度 200字/m,一代文豪:要求累计输入字数达200000)一直没有那么快的速度,就想用.net来实现. 相关技术: 1.winform基本控件 ...

  6. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第四天(非原创)

    文章大纲 一.课程介绍二.今日内容介绍三.参考资料下载四.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的使用.(2)第二天:框架的整合.后台 ...

  7. 数据库text字段存值用回车分隔

    //查询 $sql = "SELECT attr_values FROM ecs_attribute WHERE attr_id=197"; $param_sel_sms = ar ...

  8. Farseer.net轻量级开源框架 中级篇:常用的扩展方法

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: BasePage.BaseController.BaseHandler.BaseM ...

  9. chpasswd - 成批更新用户的口令

    总览 chpasswd [-e] 描述 chpasswd 从系统的标准输入读入用户的名称和口令,并利用这些信息来更新系统上已存在的用户的口令.在没有用 -e 这个开关选项的情况下,口令将按明文的形式接 ...

  10. windos快捷键

    F1帮助 F2改名 F3搜索 F4地址 F5刷新 F6切换 F10菜单 CTRL+A全选 CTRL+C复制 CTRL+X剪切 CTRL+V粘贴 CTRL+Z撤消 CTRL+O打开 SHIFT+DELE ...