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. 写一段方便的SQL 循环查每一天的数据

    declare @recd int,@i int,@a int,@count int,@day1 date,@day2 date,@days int set @day1='2014-8-24' set ...

  2. Gprinter Android SDK V2.1 使用说明

    下载:http://download.csdn.net/download/abc564643122/8872249

  3. asp.net中利用session对象传递、共享数据[session用法]

    下面介绍Asp.net中利用session对象传递.共享数据用法: 1.传递值: 首先定义将一个文本值或单独一个值赋予session,如下: session[“name”]=textbox1.text ...

  4. LED灯开关电路

    “灯控项目”中LED灯开关控制电路,LED供电电压12V,工作电流200mA. 电路图

  5. App升级时数据库的迁移更新

    前一段时间在进行App升级的时候,由于一开始版本初期没有考虑完善,导致走了很多弯路,后来经过自己的一些思考,总结出了一些在app升级的时候,数据库内文件同步保持更新的经验,希望能给大家带来帮助. 总体 ...

  6. Xcode4快速Doxygen文档注释 — 简明图文教程

    转自:http://blog.csdn.net/totogo2010/article/details/9100767 准备2个文件: 文件一,ThisService.app 文件二,Doxygen.r ...

  7. win8 需要管理员权限才能删除此应用程序

    在win8系统里面 安装了 dotfuscator,安装在c盘,结果发现运行是提示需要有管理员权限,想重新删除后安装也不行,删除的时候提示也需要管理员权限,网上查了一下资料,windows自xp开始, ...

  8. Linux文件/目录权限整理

  9. Silverlight实用窍门系列:47.Silverlight中元素到元素的绑定,以及ObservableCollection和List的使用区别

    问题一:在某一些情况下,我们使用MVVM模式的时候,对于某一个字段(AgeField)需要在前台的很多个控件(A.B.C.D.E)进行绑定,但是如何能够让我们后台字段名改变的时候能够非常方便的改变所有 ...

  10. JDK自带方法实现RSA数字签名

    JDK 6只支持MD2withRSA, MD5withRSA, SHA1withRSA 其他的如SHA512withRSA需要第三方包支持,如BC(bouncy castle) --20151126 ...