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
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even

Sample Output

K is the counterfeit coin and it is light. 

Source

 
 
#include <stdio.h>
#include <string.h> struct Balance
{
char left[], right[], status[];
}balance[]; bool view[]; int main()
{
int light[], heavy[], t;
scanf("%d", &t);
while(t--)
{
memset(view, , sizeof(view));
memset(light, , sizeof(light));
memset(heavy, , sizeof(heavy));
for(int i = ; i < ; i++)
{
scanf("%s %s %s", balance[i].left, balance[i].right, balance[i].status);
if(balance[i].status[] == 'e')
{
for(int j = ; balance[i].left[j]; j++)
view[balance[i].left[j]-'A'] = ;
for(int j = ; balance[i].right[j]; j++)
view[balance[i].right[j]-'A'] = ;
}
} for(int i = ; i < ; i++)
{
if(balance[i].status[] == 'u')
{
for(int j = ; balance[i].left[j] != '\0'; j++)
if(view[balance[i].left[j]-'A'] == )
heavy[i] |= ( << (balance[i].left[j]-'A')); for(int j = ; balance[i].right[j] != '\0'; j++)
if(view[balance[i].right[j]-'A'] == )
light[i] |= ( << (balance[i].right[j]-'A'));
}
else if(balance[i].status[] == 'd')
{
for(int j = ; balance[i].left[j] != '\0'; j++)
if(view[balance[i].left[j]-'A'] == )
light[i] |= ( << (balance[i].left[j]-'A')); for(int j = ; balance[i].right[j] != '\0'; j++)
if(view[balance[i].right[j]-'A'] == )
heavy[i] |= ( << (balance[i].right[j]-'A'));
}
} int heavy_final = -, light_final = -;
for(int i = ; i < ; i++)
{
if(light[i] != )
light_final &= light[i];
if(heavy[i] != )
heavy_final &= heavy[i];
} if(light_final > )
{
int x = ;
while((light_final & ( << x)) == )
x ++;
printf("%c is the counterfeit coin and it is light.\n", 'A'+x);
}
else
{
int x = ;
while((heavy_final & ( << x)) == )
x ++;
printf("%c is the counterfeit coin and it is heavy.\n", 'A'+x);
}
}
return ;
}

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

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

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

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

  4. 深入理解计算机系统(2.2)---布尔代数以及C语言上的位运算

    布尔代数上的位运算 布尔代数是一个数学知识体系,它在0和1的二进制值上演化而来的. 我们不需要去彻底的了解这个知识体系,但是里面定义了几种二进制的运算,却是我们在平时的编程过程当中也会遇到的.这四种运 ...

  5. POJ 2777 Count Color(线段树+位运算)

    题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...

  6. POJ - 3074 Sudoku (搜索)剪枝+位运算优化

    In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...

  7. [poj 1185] 炮兵阵地 状压dp 位运算

    Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用&quo ...

  8. leetcode上的位运算

    136-只出现过一次的数字 思路:可以考虑到数字以二进制形式存储,当两个不同的数字异或的时候会是true,所以把数组里的数字都一一处理一遍就可以了. class Solution { public: ...

  9. C语言中的位运算的技巧

    一.位运算实例 1.用一个表达式,判断一个数X是否是2的N次方(2,4,8,16.....),不可用循环语句. X:2,4,8,16转化成二进制是10,100,1000,10000.如果减1则变成01 ...

随机推荐

  1. [课堂实践与项目]手机QQ客户端--4期(SQLite的加入,注册,找回,登录界面的修改):建立关于QQ注册类,使用SQLite进行存储,

    经过昨天下午和今天上午的不懈努力,终于通过了SQLite的学习. 我们现在这里定义一个有关SQLIte的封装类,便于我在后面的用户注册,用户密码找回,和登录界面的使用 1.首先我们看看我们建立的use ...

  2. [置顶] 文件io(一)--unix环境高级编程读书笔记

    unix-like(后面以linux为例)系统中的文件操作只需要五个函数就足够了,open.close.read.write以及lseek.这些操作被称为不带缓存的io,这里有必要说一下带缓存和不带缓 ...

  3. Linux下l2tp客户端xl2tpd的安装配置

    1.下载xl2tp http://pkgs.org/search/?query=xl2tp&type=smart选择相应的版本进行下载 2.安装xl2tp和ppp yum install xl ...

  4. thymelef 布局 fragment

    需求:布局页面, 把首页分成四个页面: header  footer ,content ,aside ,从githua 下载的原型, 所有内容是在一起的,这里拆分, 重用, 减少代码量 做法: 新建页 ...

  5. 跟我一步一步开发自己的Openfire插件

    http://www.blogjava.net/hoojo/archive/2013/03/07/396146.html 跟我一步一步开发自己的Openfire插件 这篇是简单插件开发,下篇聊天记录插 ...

  6. pugixml

    http://www.firedragonpzy.com.cn/index.php/archives/3227 有关cocos2d-x的xml文件读取问题

  7. win2003 安装itunes ----iphone4s

    工具包下载:http://download.csdn.net/detail/trassion/5852663 在windows 2003 上安装itunes 8.2.0.10 ,会提示 AppleMo ...

  8. OC学习笔记[注意事项]

    alloc  new  retain之后都必须要调用release方法 计数器要变只有这几种方法 retain release alloc new copy方法才会使计数器改变,谁想用人家对象,就对他 ...

  9. Android(java)学习笔记180:Android MediaPlayer 播放prepareAsync called in state 8解决办法

    使用android MediaPlayer播放音频文件时,有时会出现prepareasync called in state 8错误. 以下方法可以避免这个异常出现.  第一种方法: private ...

  10. txt无法正常保存正文的解决办法

    最近遇到一个问题,txt文档中写了中文,则保存的时候 就会提示“该文件含有unicode格式字符,当文件保存为ANST编码文本文件时,该字符将会丢失”.虽然有解决办法,但不彻底,用起来总是很费劲,研究 ...