POJ 1013 Counterfeit Dollar
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 36206 | Accepted: 11561 |
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.
题目大意:有12枚硬币,里面有一个是假的,称三次,判断哪枚是假的,并且判断假的比真的轻还是重。
解题方法:直接枚举,总共有24中情况,即每一枚硬币都有可能比真的轻或者重,枚举这24种情况,即可得出答案。
#include <stdio.h>
#include <string.h> int main()
{
char str1[], str2[], balance[];
int nCase, ans[];//ans用于记录每种情况满足的次数有多少次
scanf("%d", &nCase);
while(nCase--)
{
memset(ans, , sizeof(ans));
for (int i = ; i < ; i++)
{
scanf("%s%s%s", str1, str2, balance);
//j等于0到11依次表示A-L中假币轻,j等于12到23依次表示A-L中假币重
for (int j = ; j < ; j++)
{
switch(balance[])
{
case 'e':
{
bool flag = true;
//如果假币轻则在两边都应该找不到该硬币
if (j < )
{
for (int k = ; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j)
{
flag = false;
}
}
for (int k = ; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j)
{
flag = false;
}
}
}
//如果假币重则在两边都应该找不到该硬币
else
{
for (int k = ; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j - )
{
flag = false;
}
}
for (int k = ; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j - )
{
flag = false;
}
}
}
//如果两边都没找到,则说明满足条件
if (flag)
{
ans[j]++;
}
}
break;
case 'u':
{
bool flag = false;
//如果左边重则轻的假币放在右边
if (j < )
{
for (int k = ; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j)
{
flag = true;
}
}
}
//如果左边重则重的假币放在左边
else
{
for (int k = ; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j - )
{
flag = true;
}
}
}
if (flag)
{
ans[j]++;
}
}
break;
case 'd':
{
bool flag = false;
//如果右边重,则轻的假币放在左边
if (j < )
{
for (int k = ; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j)
{
flag = true;
}
}
}
//如果右边重,则重的假币放在右边
else
{
for (int k = ; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j - )
{
flag = true;
}
}
}
if (flag)
{
ans[j]++;
}
}
break;
}
}
}
for (int i = ; i < ; i++)
{
//ans[i] == 3说明这种情况满足三次称量的结果
if (ans[i] == )
{
//假币比真币轻
if (i < )
{
printf("%c is the counterfeit coin and it is light.\n", i + 'A');
}
//假币比真币重
else
{
printf("%c is the counterfeit coin and it is heavy.\n", i - + 'A');
}
break;
}
}
}
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 集合上的位运算
Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...
- 思维+模拟--POJ 1013 Counterfeit Dollar
Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver d ...
- POJ 1013:Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42028 Accepted: 13 ...
- 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 ...
- POJ 1013 小水题 暴力模拟
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35774 Accepted: 11 ...
随机推荐
- spring mvc ajax 提交复杂数组类型
The server refused this request because the request entity is in a format not supported by the reque ...
- CLIQUE 聚类算法以及Java实现+多线程
CLIQUE(Clustering In QUEst)是一种简单的基于网格的聚类方法,用于发现子空间中基于密度的簇.CLIQUE把每个维划分成不重叠的区间,从而把数据对象的整个嵌入空间划分成单元.它使 ...
- Cmd命令
write----------写字板 mspaint--------画图板 mobsync--------同步命令 eudcedit-------造字程序 dvdplay--------DVD播放器 ...
- Kafka重复消费和丢失数据研究
Kafka重复消费原因 底层根本原因:已经消费了数据,但是offset没提交. 原因1:强行kill线程,导致消费后的数据,offset没有提交. 原因2:设置offset为自动提交,关闭kafka时 ...
- DOS命令中的For
前几天做安装包,需要获取某个目录下所有文件夹的名字和路径.本来想用C++写段代码,后来想起DOS有可能满足需求,于是研究了一下FOR命令,竟然非常容易的达到目的,看来小看了DOS这个门口的东西. 说下 ...
- PHP常用函数封装
//二分查找function bin_sch($array, $low, $high, $k){ if ($low <= $high) { $mid = intval(($l ...
- 解决用navicate远程连接数据库出现1045 access denied for user 'root'@'localhost' using password yes
在mysql命令行中执行 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456'); GRANT ALL PRIVILEGES ON *.* ...
- INNO安装卸载自动结束进程插件使用
[Code] //安装前判断是否有进程正在运行,istask.dll文件与打包的exe文件一起function RunTask(FileName: string; bFullpath: Boolean ...
- 使用Git Bash for Windows
本篇体验Git Bash在Windows操作系统上的用法. 什么是Bash? 是一个Shell环境,Bourne Again Shell的缩写. 安装git for windows → http:// ...
- 画之国 Le tableau (2011)
在佛斯特的肚子里,靠近幽门的地方,两只阿米巴原虫快乐地生活着.他们的日子过得很舒服,从来没饿过肚子,而且根本用不着干活:因为他们就是所谓的寄生虫.这两个好朋友相处得很愉快,但时不时也会争论不休,因为他 ...