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.

水题,思维,模拟的方式比较精巧。

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int n, cnt, max;
string s1, s2, temp;
int flag[12];
cin>>n;
while (n--)
{
memset(flag, 0, sizeof(flag));
for (int t = 1; t <= 3; ++t)
{
cin >> s1 >> s2 >> temp;
if (temp=="even")
{
for (int i=0; i <s1.size(); ++i)
{
flag[s1[i] - 'A'] = 10;
flag[s2[i] - 'A'] = 10;
}
}
else if (temp== "up")
{
for (int i=0; i <s1.size(); ++i)
{
if (flag[s1[i] - 'A'] != 10)
++flag[s1[i] - 'A'];
if (flag[s2[i] - 'A'] != 10)
--flag[s2[i] - 'A'];
}
}
else
{
for (int i=0; i <s1.size(); ++i)
{
if (flag[s1[i] - 'A'] != 10)
--flag[s1[i] - 'A'];
if (flag[s2[i] - 'A'] != 10)
++flag[s2[i] - 'A'];
}
}
}
max =cnt = 0;
for (int i=0; i < 12; ++i)
{
if (flag[i] == 10)
continue;
if (max <= abs(flag[i]))
{
max = abs(flag[i]);
cnt = i;
}
}
if (flag[cnt] > 0)
printf("%c is the counterfeit coin and it is heavy.\n", 'A' + cnt);
else
printf("%c is the counterfeit coin and it is light.\n", 'A' + cnt);
}
return 0;
}

思维+模拟--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 集合上的位运算

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

  4. POJ 1013:Counterfeit Dollar

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42028   Accepted: 13 ...

  5. POJ 1013 小水题 暴力模拟

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

  6. Counterfeit Dollar 分类: POJ 2015-06-12 15:28 19人阅读 评论(0) 收藏

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41559   Accepted: 13 ...

  7. POJ1013 Counterfeit Dollar

    题目来源:http://poj.org/problem?id=1013 题目大意:有12枚硬币,其中有一枚假币.所有钱币的外表都一样,所有真币的重量都一样,假币的重量与真币不同,但我们不知道假币的重量 ...

  8. Counterfeit Dollar -----判断12枚钱币中的一个假币

     Counterfeit Dollar Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  9. poj1013.Counterfeit Dollar(枚举)

    Counterfeit Dollar Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 415  Solved: 237 Description Sally ...

随机推荐

  1. Vue 实战项目: 硅谷外卖(1)

    第 1 章: 准备 1.1. 项目描述 1) 此项目为外卖 WebApp(SPA) 2) 包括商家, 商品, 购物车, 用户等多个子模块 3) 使用 Vue 全家桶+ES6+Webpack 等前端最新 ...

  2. PHP远程代码执行漏洞复现(CVE-2019-11043)

    漏洞描述 CVE-2019-11043 是一个远程代码执行漏洞,使用某些特定配置的 Nginx + PHP-FPM 的服务器存在漏洞,可允许攻击者远程执行代码. 向Nginx + PHP-FPM的服务 ...

  3. 自己实现一个 DFA 串模式识别器

    自己实现一个 DFA 串模式识别器 前言 这是我编译原理课程的实验.希望读完这篇文章的人即便不知道 NFA,DFA 和正规表达式是什么,也能够对它们有一个简单的理解,并能自己去实现一个能够识别特定模式 ...

  4. For循环详解

    for语句 学过c语言都对循环结构不陌生,尤其是for循环,他是C语言中最有特色的循环语句,使用最为灵活. 形式 结构:for(表达式1:表达式2:表达式3){循环体结构} 每部分的作用 表达式1:一 ...

  5. 使用Scanner接受用户键盘输入的值

    第一步导入Scanner import java.util.*; 第二步创建Scanner对象 Scanner input = new Scanner(System.in); 第二步创建Scanner ...

  6. 《JavaScript 模式》读书笔记(6)— 代码复用模式2

    上一篇讲了最简单的代码复用模式,也是最基础的,我们普遍知道的继承模式,但是这种继承模式却有不少缺点,我们下面再看看其它可以实现继承的模式. 四.类式继承模式#2——借用构造函数 本模式解决了从子构造函 ...

  7. Css3 新增的属性以及使用

    Css3基础操作 . Css3? css3事css的最新版本 width. heith.background.border**都是属于css2.1CSS3会保留之前 CSS2.1的内容,只是添加了一些 ...

  8. sublime text3配置html环境

    1.安装View in Browser 2.配置快捷键 [1]Preferences—Key Bindings—User. [2]插入代码 [ //ie { "keys": [&q ...

  9. DataGridView行号发生变化 使用的事件

    DataGridView并没有这么专门为行号发生变化时触发的事件,我们只能用SelectionChanged和CurrentCellChanged做些设置后实现. 1.使用SelectionChang ...

  10. D. Ehab the Xorcist

    题意: 略: 感觉被演了一波,这是CFdiv2吗? 算是这个构造题吧. 1 首先我们可以将u进行二进制拆分来考虑.加入u>v那么小与v的那些数在怎么拼接也无法使异或值为u. 比如二进制U=1 0 ...