Counterfeit Dollar
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 53112 | Accepted: 16576 |
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.
题意:有12个硬币,称重3次,找出假币并写明假币偏轻or偏重
思路:even的两边一定都是真币,在up或down两边都出现过的也一定是真币
用vis数组做标记,初始值为0,为真的直接标成100,可以把偏重的++,偏轻的--。(两边都出现的++--后为0)
最后循环一遍12个硬币,取绝对值最大的硬币,因为有可能有一个硬币只出现了一次,偏重后没有再判了,会误判。
比如(E是假币)
ABCD EFGH up
ABCE FGHI down
ABCF GHIK even
如果不是++和--,而是简单的1和-1,那么D会被判成假币。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define LL long long
int main()
{
int n,i,j,k;
string s1,s2,s;
scanf("%d",&n);
while(n--)
{
int vis[30]={0};
for(i=0;i<3;i++)
{
cin>>s1>>s2>>s;
if(s=="even")
{//两边肯定都是真的
for(j=0;j<s1.length();j++)
vis[s1[j]-'A']=100;
for(j=0;j<s2.length();j++)
vis[s2[j]-'A']=100;
}
else if(s=="up")
{//+为偏重 -为偏轻
for(j=0;j<s1.length();j++)
{
if(vis[s1[j]-'A']!=100)
vis[s1[j]-'A']++;
}
for(j=0;j<s2.length();j++)
{
if(vis[s2[j]-'A']!=100)
vis[s2[j]-'A']--;
}
}
else
{
for(j=0;j<s1.length();j++)
{
if(vis[s1[j]-'A']!=100)
vis[s1[j]-'A']--;
}
for(j=0;j<s2.length();j++)
{
if(vis[s2[j]-'A']!=100)
vis[s2[j]-'A']++;
}
}
}
int max=0,maxi=0;
for(i=0;i<12;i++)
{
if(vis[i]==100||vis[i]==0)continue;
if(abs(vis[i])>max)
{
max=abs(vis[i]);
maxi=i;
}
}
if(vis[maxi]>0)
printf("%c is the counterfeit coin and it is heavy.\n",maxi+'A');
else
printf("%c is the counterfeit coin and it is light.\n",maxi+'A');
}
}