Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total 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.
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枚硬币,所以进行枚举就行。
此题意思是将十二枚硬币,分别测量三次,求出哪个是假币,
假币有可能比真币轻,或重,
所以在考虑情况的时候,两边相等的里面肯定没有假币,
在假币轻的情况下,假币在中的那边肯定不可能。
代码:
#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;
}