2021-03-27

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;
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初,梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值