POJ 1013 Counterfeit Dollar 集合上的位运算
Description
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
Output
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 集合上的位运算的更多相关文章
- Poj 1013 Counterfeit Dollar / OpenJudge 1013(2692) 假币问题
1.链接地址: http://poj.org/problem?id=1013 http://bailian.openjudge.cn/practice/2692 http://bailian.open ...
- POJ 1013 Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36206 Accepted: 11 ...
- 思维+模拟--POJ 1013 Counterfeit Dollar
Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver d ...
- 深入理解计算机系统(2.2)---布尔代数以及C语言上的位运算
布尔代数上的位运算 布尔代数是一个数学知识体系,它在0和1的二进制值上演化而来的. 我们不需要去彻底的了解这个知识体系,但是里面定义了几种二进制的运算,却是我们在平时的编程过程当中也会遇到的.这四种运 ...
- POJ 2777 Count Color(线段树+位运算)
题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...
- POJ - 3074 Sudoku (搜索)剪枝+位运算优化
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...
- [poj 1185] 炮兵阵地 状压dp 位运算
Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用&quo ...
- leetcode上的位运算
136-只出现过一次的数字 思路:可以考虑到数字以二进制形式存储,当两个不同的数字异或的时候会是true,所以把数组里的数字都一一处理一遍就可以了. class Solution { public: ...
- C语言中的位运算的技巧
一.位运算实例 1.用一个表达式,判断一个数X是否是2的N次方(2,4,8,16.....),不可用循环语句. X:2,4,8,16转化成二进制是10,100,1000,10000.如果减1则变成01 ...
随机推荐
- 【Android - 基础】之PopupWindow的使用
创建一个类继承自PopupWindow,编写自定义的PopupWindow类.示例代码如下: import android.app.Activity; import android.graphics. ...
- 从struts2.1开始Convention零配置
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...
- shell查找文件并删除
-mtime 0 表示文件改动时间距离当前为0天的文件.即距离当前时间不到1天(24小时)以内的文件. -mtime 1 表示文件改动时间距离当前为1天的文件,即距离当前时间1天(24小时-48小时) ...
- sql执行计划解析案例(二)
sql执行计划解析案例(二) 今天是2013-10-09,本来以前自己在专注oracle sga中buffer cache 以及shared pool知识点的研究.但是在研究cache buffe ...
- 安装Win7和Ubuntu12.04双系统后,意外删除Ubuntu12.04引导文件,出现error:unknown filesystem;grub rescue>错误的解决方案
很久之前在Win7基础上安装了Ubuntu12.04系统,采用硬盘安装的方法.分了1个10G的硬盘分区F盘用于存放Ubuntu12.04的引导文件,其实完全可以制作一个Ubuntu12.04的U盘启动 ...
- Day05 - Python 常用模块
1. 模块简介 模块就是一个保存了 Python 代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码. 模块也是 Python 对象,具有随机的名字属性用来绑定或引用. 下例是个简单的模 ...
- maven 学习1 -安装maven 并执行编译命令
一.maven 下载与安装(安装好jdk的前提下) 1.下载地址:http://maven.apache.org/download.cgi (选择最新的zip版本),下载完毕后解压 2.安装:系统p ...
- SQL循环+游标
/****** Script for SelectTopNRows command from SSMS ******/use DB declare @id bigint DECLARE cur ...
- Merge OUTPUT 高级用法综合写的一个MergeTab的存储过程
因为工作中常用到 合并两张表中的数据,主要是写下来给自己备忘,T-SQL 中 MERGE 的用法 WHEN MATCHED THEN UPDATE -- 中加了 后面要更新的列是否都相等,如果相等就没 ...
- SQL Server 2008文件与文件组的关系
此文章主要向大家讲述的是SQL Server 2008文件与文件组,其中包括文件和文件组的含义与关系,文件.文件组在实践应用中经常出现的问题,查询文件组和文件语句与MSDN官方解释等相关内容的介绍. ...