POJ 1013 题解
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.
题目大概意思
有12个银币,其中有1个假币,11个真币。测量三次可以确定哪一个是假币,并且可以获得假币的重量是重的还是轻的。
题目分析
由于题目说了一定能找到假币,且假币是唯一额,所以可以先利用测量平衡的样例,将所有的真币删除,然后如果测量为up,则说明假币在右边,则将右边的所有币重量加1,左边的币重量减1,反之同理。
然后统计每个币重量,绝对值最大就是假币。
#include<bits/stdc++.h>
using namespace std;
int flag[12];
int weight[12];
void init(){
for(int i=0;i<12;i++){
flag[i]=1;
weight[i]=0;
}
}
int abs(int a){
return a>0?a:-a;
}
int main(){
int t;
cin>>t;
while(t--){
init();
string a,b,c;
for(int i=0;i<3;i++){
cin>>a>>b>>c;
if(c=="even"){
string s=a+b;
for(int j=0;j<s.size();j++){
flag[s[j]-'A']=0;
}
}
else{
if(c=="up"){
for(int j=0;j<a.size();j++){
weight[a[j]-'A']++;
}
for(int j=0;j<b.size();j++){
weight[b[j]-'A']--;
}
}
else{
for(int j=0;j<a.size();j++){
weight[a[j]-'A']--;
}
for(int j=0;j<b.size();j++){
weight[b[j]-'A']++;
}
}
}
}
int max=-1;
int f=0;
for(int i=0;i<12;i++){
if(abs(weight[i])>max&&flag[i]==1){
max=abs(weight[i]);
f=i;
}
}
if(weight[f]<0){
char c=f+'A';
cout<<c<<" is the counterfeit coin and it is light. "<<endl;
}
if(weight[f]>0){
char c=f+'A';
cout<<c<<" is the counterfeit coin and it is heavy. "<<endl;
}
}
}