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 ...
随机推荐
- [JS10] 获取时间
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Wijmo 2016 V2 强势发布!
Angular 2 支持 Wijmo 对 Angular 2 提供了全面的支持.我们一直在紧跟Angular 2 开发团队的步伐,对其发布的最新候选版本提供支持. 了解更多关于Angular 2 的支 ...
- paip.提升效率--批量变量赋值 “多元”赋值
paip.提升效率--批量变量赋值 "多元"赋值 ##石麻是批量变量赋值. 为一组变量赋值. 例子 1 <?php $my_array = array("Dog&q ...
- MapReduce之单词计数
最近在看google那篇经典的MapReduce论文,中文版可以参考孟岩推荐的 mapreduce 中文版 中文翻译 论文中提到,MapReduce的编程模型就是: 计算利用一个输入key/value ...
- 开发ERP软件应该遵守的22条规则
总结一下做管理软件,有哪些项是经过检验的条款,必须遵守的. 界面篇 1 要保存用户的偏号(profile/favourite). ASP.NET 2.0引入此功能,当用户修改默认的控件的属性时,框架 ...
- iOS开发实用技巧—在手机浏览器头部弹出app应用下载提示
iOS开发实用技巧—在手机浏览器头部弹出app应用下载提示 本文介绍其简单使用: 第一步:在本地建立一个访问的服务端. 打开本地终端,在本地新建一个文件夹,在该文件夹中存放测试的html页面. ...
- 禁用iOS的UIView长按默认操作
* { -webkit-touch-callout: none; -webkit-user-select: none; -webkit-tap-highlight-color: rg ...
- 嵌入式OS的现状、智能的物联网与未来的机器人
嵌入式开发是一个低调的领域.相比Web开发和企业级开发,嵌入式开发这一领域似乎很少在互联网上发出声音.随着智能设备的兴起,智能手环.手表.眼镜.灯泡等产品成为互联网企业的下一个目标,而物联网这一概念再 ...
- Java 中的instanceof简单讲解
Java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. 用法:resu ...
- HEXO+PAGE 搭建个性博客
新博客地址: http://javen205.oschina.io https://javen205.github.io Hexo 是高效的静态站点生成框架,她基于 Node.js. 通过 Hexo ...