题目链接:http://poj.org/problem?id=1013
Counterfeit Dollar
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 39609 | Accepted: 12622 |
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.
这题其实不难,最近没状态就这么一题,连WA带PE 错了十几次,最后好容易找出来 是if(XXXXXX)后边{code; }前边多了一个分号,丫的 if 白写了。
最近越写,越没状态。
贴代码:
#include <stdio.h>
#include <string.h>
int time[15], la, lb;
char a[15], b[15], c[15];
const int INF = 100000;
// int mark[15];
/**
void find()
{
int i;
memset(mark,0,sizeof(mark));
for( i = 0; i < la; i++)
{
mark[a[i] - 'A'] = 1;
}
for( i = 0; i < lb; i++)
{
mark[b[i] - 'A'] = 1;
}
for( i = 0; i < 12; i++)
if(!mark[i])
time[i] = INF;
}
**///最开始写的find()函数 用于标记up down 时 其他没用到的真的钱币 后来发现没用 不写也行
void judge()
{
int i;
la = strlen(a);
lb = strlen(b);
if(!strcmp(c,"even"))
{
for( i = 0; i < la; i++)
{
time[a[i] - 'A'] = INF;
}
for( i = 0; i < lb; i++)
{
time[b[i] - 'A'] = INF;
}
}
else if(!strcmp(c,"up"))
{
//find();
for( i = 0; i < la; i++)
{
if(time[a[i] - 'A'] != INF)
time[a[i] - 'A']++;
}
for( i = 0; i < lb; i++)
{
if(time[b[i] - 'A'] != INF)
time[b[i] - 'A']--;
}
}
else if(!strcmp(c,"down"))
{
//find();
for( i = 0; i < la; i++)
{
if(time[a[i] - 'A'] != INF)
time[a[i] - 'A']--;
}
for( i = 0; i < lb; i++)
{
if(time[b[i] - 'A'] != INF)
time[b[i] - 'A']++;
}
}
}
int Abs( int x)
{
return x > 0 ? x : -x;
}
int main()
{
int T, i, k, m;
scanf("%d",&T);
while(T--)
{
memset(time,0,sizeof(time));
scanf("%s%s%s",a,b,c);
judge();
scanf("%s%s%s",a,b,c);
judge();
scanf("%s%s%s",a,b,c);
judge();
k = m = 0;
for( i = 0; i < 12; i++)
{
if(time[i] == INF)continue;
if(k < Abs(time[i])) <span style="background-color: rgb(255, 255, 255);"><span style="color:#009900;">// 就是这 开始多写了 ; 错了十几次才找到</span></span>
{
k = Abs(time[i]);
m = i;
}
}
if(time[m] < 0)printf("%c is the counterfeit coin and it is light.\n",'A' + m);
else printf("%c is the counterfeit coin and it is heavy.\n",'A' + m);
}
return 0;
}
运行结果: