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 ;
}

最新文章

  1. PHP array_multisort—对多个数组或多维数组进行排序
  2. 0e开头md5汇总
  3. Cheatsheet: 2014 04.01 ~ 04.30
  4. asynDBCenter(修改)
  5. 退出telnet
  6. (1)I2c的简介和特性
  7. 交叉编译安装ARM平台上的Qt
  8. UVA 11214 Guarding the Chessboard
  9. E=MC2 - 搜搜百科
  10. java--线程的睡眠sleep()
  11. tox环境安装
  12. 初始css
  13. AngularJs 学习笔记(三)依赖注入
  14. plugin.go 源码阅读
  15. Network POJ - 3417(LCA+dfs)
  16. 计算机信息类ComputerInfo(车)
  17. 【Apache】Apache的安装和配置
  18. MySql数据库事务正常提交,回滚失败
  19. HDU - 5201 :The Monkey King (组合数 &amp; 容斥)
  20. conda设置Python虚拟环境

热门文章

  1. jquerymobile知识点:实现toolbar下方显示,自定义图标!
  2. 关于Struts框架简介
  3. 懂,你的App生,不懂,死!
  4. static inner class 什么时候被加载
  5. majikan
  6. sqlite 获取数据库中的所有表
  7. Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)
  8. index页面数据展示为设定的命名
  9. 4G上网卡NIDS拨号之Rmnet驱动
  10. Node.js + Express + Mongodb 开发搭建个人网站(三)