思维+模拟--POJ 1013 Counterfeit Dollar
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的更多相关文章
- 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 集合上的位运算
Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...
- POJ 1013:Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42028 Accepted: 13 ...
- POJ 1013 小水题 暴力模拟
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35774 Accepted: 11 ...
- Counterfeit Dollar 分类: POJ 2015-06-12 15:28 19人阅读 评论(0) 收藏
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41559 Accepted: 13 ...
- POJ1013 Counterfeit Dollar
题目来源:http://poj.org/problem?id=1013 题目大意:有12枚硬币,其中有一枚假币.所有钱币的外表都一样,所有真币的重量都一样,假币的重量与真币不同,但我们不知道假币的重量 ...
- Counterfeit Dollar -----判断12枚钱币中的一个假币
Counterfeit Dollar Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- poj1013.Counterfeit Dollar(枚举)
Counterfeit Dollar Time Limit: 1 Sec Memory Limit: 64 MB Submit: 415 Solved: 237 Description Sally ...
随机推荐
- 【翻译】OpenVINO Pre-Trained 预训练模型介绍
OpenVINO 系列软件包预训练模型介绍 本文翻译自 Intel OpenVINO 的 "Overview of OpenVINO Toolkit Pre-Trained Models& ...
- Java第一天,带你走进编程的世界,我的第一个程序
要想彻底了解Java是什么,我就得首先了解编程语言的发展史.编程语言最初的形势是"0101......"数据编程,也就是机器语言.机器语言可以说是一种几乎没有人能够看懂的编程语言, ...
- tf.get_variable
使用tf.get_variable()时,如果检测到命名冲突,系统不会处理冲突,而会报错. 如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的话,就创建一个新的. tf.get_vari ...
- tcp长连接、短连接、连接池的思考
在基于tcp的 rcp实现方式中,有如下几种选择: 1. 长连接:同步和异步方式. 同步方式下客户端所有请求共用同一连接,在获得连接后要对连接加锁,在读写结束后才解锁释放连接,性能低下,基本很少采用, ...
- android29之UI控件的抽屉式实现方法之一(DrawerLayout和NavigationView)
添加依赖 implementation 'com.google.android.material:material:1.2.0-alpha06' 在Layout中创建两个Xml布局文件,header. ...
- SpringCloud入门(十): Config 统一配置中心
SpringCloud Config 简介 在分布式系统中,由于服务组件过多,为了方便争对不通的环境下的服务配置文件统一管理,实时更新,所以出现了分布式配置中心组件.市面上开源的配置中心有很多,360 ...
- 资料整理:python自动化测试——操作测试对象
文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:爱吃米饭的猪 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...
- HashMap主要方法源码分析(JDK1.8)
本篇从HashMap的put.get.remove方法入手,分析源码流程 (不涉及红黑树的具体算法) jkd1.8中HashMap的结构为数组.链表.红黑树的形式 (未转化红黑树时) (转 ...
- Sprint1规划暨first stand up meeting
实际上,我们关于工程分配和接口实现的讨论已经好几周了,队(shen)长(xian)大人三令五申,先把接口确定下来,数据格式很重要云云~顺便accent一下,utf-8[虽然我并不太明白为什么要这么干但 ...
- get 获取方式练习题及dom基础
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...