Algorithm学习笔记 --- Counterfeit Dollar
来源:互联网 发布:商行天下软件 编辑:程序博客网 时间:2024/06/05 06:12
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 38464 Accepted: 12287
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.
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
East Central North America 1998
解题分析:
对于此题,只有12枚硬币,所以进行枚举就行。
对于此题,只有12枚硬币,所以进行枚举就行。
此题意思是将十二枚硬币,分别测量三次,求出哪个是假币,
假币有可能比真币轻,或重,
所以在考虑情况的时候,两边相等的里面肯定没有假币,
在假币轻的情况下,假币在中的那边肯定不可能。
代码:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[10], str2[10], balance[10];
int nCase, ans[25];//ans用于记录每种情况满足的次数有多少次
scanf("%d", &nCase);
while(nCase--)
{
memset(ans, 0, sizeof(ans));
for (int i = 0; i < 3; i++)
{
scanf("%s%s%s", str1, str2, balance);
//j等于0到11依次表示A-L中假币轻,j等于12到23依次表示A-L中假币重
for (int j = 0; j < 24; j++)
{
switch(balance[0])
{
case 'e':
{
bool flag = true;
//如果假币轻则在两边都应该找不到该硬币
if (j < 12)
{
for (int k = 0; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j)
{
flag = false;
}
}
for (int k = 0; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j)
{
flag = false;
}
}
}
//如果假币重则在两边都应该找不到该硬币
else
{
for (int k = 0; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j - 12)
{
flag = false;
}
}
for (int k = 0; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j - 12)
{
flag = false;
}
}
}
//如果两边都没找到,则说明满足条件
if (flag)
{
ans[j]++;
}
}
break;
case 'u':
{
bool flag = false;
//如果左边重则轻的假币放在右边
if (j < 12)
{
for (int k = 0; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j)
{
flag = true;
}
}
}
//如果左边重则重的假币放在左边
else
{
for (int k = 0; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j - 12)
{
flag = true;
}
}
}
if (flag)
{
ans[j]++;
}
}
break;
case 'd':
{
bool flag = false;
//如果右边重,则轻的假币放在左边
if (j < 12)
{
for (int k = 0; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j)
{
flag = true;
}
}
}
//如果右边重,则重的假币放在右边
else
{
for (int k = 0; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j - 12)
{
flag = true;
}
}
}
if (flag)
{
ans[j]++;
}
}
break;
}
}
}
for (int i = 0; i < 24; i++)
{
//ans[i] == 3说明这种情况满足三次称量的结果
if (ans[i] == 3)
{
//假币比真币轻
if (i < 12)
{
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 - 12 + 'A');
}
break;
}
}
}
return 0;
}
#include <string.h>
int main()
{
char str1[10], str2[10], balance[10];
int nCase, ans[25];//ans用于记录每种情况满足的次数有多少次
scanf("%d", &nCase);
while(nCase--)
{
memset(ans, 0, sizeof(ans));
for (int i = 0; i < 3; i++)
{
scanf("%s%s%s", str1, str2, balance);
//j等于0到11依次表示A-L中假币轻,j等于12到23依次表示A-L中假币重
for (int j = 0; j < 24; j++)
{
switch(balance[0])
{
case 'e':
{
bool flag = true;
//如果假币轻则在两边都应该找不到该硬币
if (j < 12)
{
for (int k = 0; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j)
{
flag = false;
}
}
for (int k = 0; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j)
{
flag = false;
}
}
}
//如果假币重则在两边都应该找不到该硬币
else
{
for (int k = 0; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j - 12)
{
flag = false;
}
}
for (int k = 0; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j - 12)
{
flag = false;
}
}
}
//如果两边都没找到,则说明满足条件
if (flag)
{
ans[j]++;
}
}
break;
case 'u':
{
bool flag = false;
//如果左边重则轻的假币放在右边
if (j < 12)
{
for (int k = 0; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j)
{
flag = true;
}
}
}
//如果左边重则重的假币放在左边
else
{
for (int k = 0; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j - 12)
{
flag = true;
}
}
}
if (flag)
{
ans[j]++;
}
}
break;
case 'd':
{
bool flag = false;
//如果右边重,则轻的假币放在左边
if (j < 12)
{
for (int k = 0; k < strlen(str1); k++)
{
if (str1[k] == 'A' + j)
{
flag = true;
}
}
}
//如果右边重,则重的假币放在右边
else
{
for (int k = 0; k < strlen(str2); k++)
{
if (str2[k] == 'A' + j - 12)
{
flag = true;
}
}
}
if (flag)
{
ans[j]++;
}
}
break;
}
}
}
for (int i = 0; i < 24; i++)
{
//ans[i] == 3说明这种情况满足三次称量的结果
if (ans[i] == 3)
{
//假币比真币轻
if (i < 12)
{
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 - 12 + 'A');
}
break;
}
}
}
return 0;
}
0 0
- Algorithm学习笔记 --- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- Counterfeit Dollar
- POJ 1013 Counterfeit Dollar 笔记 模拟
- POJ 1013 Counterfeit Dollar
- 1013 Counterfeit Dollar
- pku_1013 Counterfeit Dollar
- POJ 1013 Counterfeit Dollar
- 预先队列
- org.apache.commons.codec.binary.Base64图片加密解密
- 彻底解决 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- ajax图片上传,图片异步上传,更新
- 微博平台的链路追踪及服务质量保障系统——Watchman系统
- Algorithm学习笔记 --- Counterfeit Dollar
- [译]Hadoop-2.2.0中文文档—— Shell命令
- Transaction详细介绍
- 怎么用代码改变CComboBox的下拉方式?
- COJ 1102 - You Can Say 11 题解
- nyoj 16 矩形嵌套
- BFD1从北京顺义新国展到酒仙桥特斯拉办公室
- linux服务器使用入门
- RFYTRHJ1认为今天特斯拉的上市就像7年前的iPhone一样