Programming notes and coins of the algorithm known

He said the coins
have 12 coins. Including 11 real coin and a counterfeit money. True and counterfeit
coins of different weight, but do not know counterfeit or lighter weight than the true coin. Now,
with a balance that these three currencies, the results tell you known, would you please
identify counterfeit money and counterfeit money is determined is light weight (data guarantee it will be to find
out).
 input
of the first row is the number of test data sets.
Each group has three rows of data, each row represents a weighing result. Silver label
for the AL. Each weighing result represented by three string separated by a space:
the right balance of coins placed in the balance of the coin is placed on the left balance. Its
equilibrium state with a up'',down '', or even''表示, 分 别为右端高、右端低和平衡。天平左右的硬币数总是相等 的。  输出 输出哪一个标号的银币是假币,并说明它比真币轻还是重。 例题:POJ1013 称硬币  输入样例 1 ABCD EFGH even ABCI EFJK up ABIJ EFGH even  输出样例 K is the counterfeit coin and it is light. 例题:POJ1013 称硬币  解题思路 先假定不平衡中的都是假currency and to determine the severity, then one by one scanning check, what is left is counterfeit.

```cpp
#include<iostream>
using namespace std;
char Left[3][7],Right[3][7],Result[3][7]; //3个2维数组分别存放天平左右和结果 
char  IsFake(char c ){ //返回称量结果 重就是'H' ,轻就是'L' ,真币返回0; 
	char r=0;
	for(int i=0;i<3;++i){  
		for(int j=0;j<=3;++j){
			if(Result[i][0]=='U'){
				if(Left[i][j]==c)r='H';  //出现这种情况先假设这个硬币为重的 
				if(Right[i][j]==c)r='L';
			}
			if(Result[i][0]=='D'){
				if(Left[i][j]==c)r='L';
				if(Right[i][j]==c)r='H';	
			}
		}
	}
	for(int i=0;i<3;++i)
		for(int j=0;j<=3;++j){
			if(Result[i][0]=='E'&&(Left[i][j]==c||Right[i][j]==c))r=0;//天平平衡而有该硬币出现,更正结果为0 
			if(r=='H' && Result[i][0]=='U' && Right[i][j]==c)r=0; //如果假设是重的且天平上翘而该硬币出现在右边,就否定假设, 
			if(r=='L' && Result[i][0]=='D' && Left[i][j]==c)r=0; 
		}
	return r;	
}
int main(){
	int t;
	cin>>t;
	//freopen("d\\cyb.txt","r",stdin);
	while(t--){
		for(int i=0;i<3;++i)
			cin>>Left[i]>>Right[i]>>Result[i];
		for(char c='A';c<='L';c++){ 
			char r=IsFake(c);
			if(r=='L')cout<<c<<" is the counterfeit coin and it is light.\n";
			if(r=='H')cout<<c<<" is the counterfeit coin and it is heavy.\n";
		}
	}
	return 0;
}
Released five original articles · won praise 0 · Views 57

Guess you like

Origin blog.csdn.net/qq_35076612/article/details/104434429