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. 用CRTP在C++中实现静态函数的多态

    我上一篇博客[C++的静态分发(CRTP)和动态分发(虚函数多态)的比较](http://www.cnblogs.com/fresky/p/3504241.html)介绍了如何用CRTP(Curiou ...

  2. springsecurity4+springboot 实现remember-me 发现springsecurity 的BUG

    前言:现在开发中,记住我这个功能是普遍的,用户不可能每次登录都要输入用户名密码.昨天准备用spring security的记住我功能,各种坑啊,吐血 . 先看下具体实现吧. spring securi ...

  3. Mysql : L闪存卡linux中的内核参数设置

    将 Nytro WarpDrive 加速卡配置为文件系统 本节说明的操作使您可调整 Nytro WarpDrive 加速卡,增强使用 Oracle Linux with Unbreakable Ent ...

  4. Choosing a Linux Tracer ------Brendan Gregg's Blog

    home Choosing a Linux Tracer (2015) 08 Jul 2015 Linux Tracing is Magic! A tracer is an advanced perf ...

  5. lambda与函数调用的转换

    14.38 编写一个类令其检查某个给定的string对象的长度是否与一个阈值相等.使用该对象编写程序,统计并报告在输入的文件中长度为1的单词有多少个,长度为2的单词有多少个.......长度为10的单 ...

  6. QuaZip实现多文件打包

    项目需求: 在Goldenfarm客户端中当用户选择了本地场景文件,并进行本地场景文件分析后会产生分析结果,分析结果主要包括:贴图纹理.可渲染层等,其中贴图纹理指出了在场景文件中使用到的贴图或其它文件 ...

  7. IOS Remote Notification

    1. 本地证书合成 rm *.pem echo "export cert..." openssl pkcs12 -clcerts -nokeys -out push_cert.pe ...

  8. String类的基本用法与注意点,StringBuffer类的用法

    package cn.hncu.day8; public class RegExpDemo { public static void main(String[] args) { String str ...

  9. python sklearn.linear_model.LinearRegression.score

    score(self, X, y, sample_weight=None) 作用:返回该次预测的系数R2     其中R2 =(1-u/v).u=((y_true - y_pred) ** 2).su ...

  10. Gym 100187A-Potion of Immortality

    题意:有n个药瓶,里面有一个有毒,然后每次拿兔子去试吃k瓶并且只能是k瓶,如果兔子死了就知道那瓶是毒药了,现在问你最少兔子要试吃几次. 分析:这题卡了好久,其实很简单.先考虑肯定要吃n/k次,那么剩下 ...