1013 Counterfeit Dollar: We do not want violence: beautiful determining counterfeit money (+ contains a large set of data analysis in each case)

Title Translation

Sally Jones (Sally Jones) has more than a dozen travelers silver dollar. However, only eleven silver coin is true. A coin is counterfeit, even though its color and size of silver dollars with no real difference. The weight of counterfeit coins and other coins of different weight, but Sally does not know it is heavier than the real coins or light.

Fortunately, Sally had a friend borrowed her a very accurate balance sheet. Friends will allow Sally weighed three times to find counterfeit money. For example, if Sally and two coins each weighing scales balance, the two coins she knew it was right. Now, if Sally weighed

A real coin in the coin with respect to the third, unbalanced scales, third and Sally know coins are counterfeit, she may be placed on a balance which rise or fall to distinguish it in the light or heavy.

By carefully choosing her weighed, Sally can ensure that she will pinpoint three weighing coins.

Entry

The first line of the input integer n (n> 0), is used to specify the number of cases to follow. Three-row input in each case, each line were weighed once. Sally (Sally) identified by the letters A-L for each coin. Weighing two strings of information by letters and are given on one or even-numbered. The first letter string representing the balance of the coin on the left side; the second string, the right balance of the coin. (Sally always placed on the right side of the balance and the balance of the left side of the same number of coins.) The third position in the word will tell the right side of your balance is up, down or remain the same.

Ideas analysis

Ideas: one by one to determine whether each is true gold.

1, the day when the level is even, inside gold coins are true;

2, when the balance is not even, fake gold coins in the balance, and not there are true , while the balance in the gold mark. Divided into light and heavy.

3, when a gold coin is both light and heavy when, really.

The third point is also very important, such as the next set of data, even a very weak condition, we must up from two, down in to Tell on him with AB in the above time is the heavy side, and lighter below, that he is not a decisive factor.

ABCDEF GHIJKL up 
ABHLEF GDIJKC down 
CD HA even 

The output is false coins.

Attach a set of data:

12 
ABCD EFGH even 
ABCI EFJK up 
ABIJ EFGH even 
AGHL BDEC even 
JKI ADE up 
J K even 
ABCDEF GHIJKL up 
ABC DEF even 
I J down 
ABCDEF GHIJKL up 
ABHLEF GDIJKC down 
CD HA even 
A B up 
B A down 
A C even 
A B up 
B C even 
DEFG HIJL even 
ABC DEJ down 
ACH IEF down 
AHK IDJ down 
ABCD EFGH even 
AB IJ even 
A L down 
EFA BGH down 
EFC GHD even 
BA EF down 
A B up 
A C up 
L K even 
ACEGIK BDFHJL up 
ACEGIL BDFHJK down 
ACEGLK BDFHJI down 
ACEGIK BDFHJL up 
ACEGIL BDFHJK down 
ACEGLK BDFHJI up 
sample output 
K is the counterfeit coin and it is light. 
I is the counterfeit coin and it is heavy. 
I is the counterfeit coin and it is light. 
L is the counterfeit coin and it is light. 
B is the counterfeit coin and it is light. 
A is the counterfeit coin and it is heavy. 
A is the counterfeit coin and it is light. 
L is the counterfeit coin and it is heavy. 
A is the counterfeit coin and it is light. 
A is the counterfeit coin and it is heavy. 
L is the counterfeit coin and it is light. 
K is the counterfeit coin and it is heavy.
#include<iostream>
#include<map>
#include<string>
#include<string.h>
using namespace std;

int coins[12];//条件码 0:待确定 1:真的 2:未曾判断  所有硬币最后只能有一个待确定,就是假的
int cnt = 0, weight[12];//已经确认为真的数目,weight:如果是假的,是重:1还是轻:-1。
int main() {
	int n; cin >> n;
	string s1, s2, s3;
	while (n--) {
		for (int i = 0; i < 12; i++)coins[i] = 2;
		memset(weight, 0, sizeof(weight));
		for (int i = 0; i < 3; i++) {
			cin >> s1 >> s2 >> s3;
			if (s3[0] == 'e') {
				for (int j = 0; j < s1.size(); j++)coins[s1[j] - 'A'] = 1;
				for (int j = 0; j < s2.size(); j++)coins[s2[j] - 'A'] = 1;
			}
			else if (s3[0] == 'u') {//右边轻
				for (int j = 0; j < s1.size(); j++)
				{//不是真的就可能是假的
					if (coins[s1[j] - 'A'] == 2)coins[s1[j] - 'A'] = 0, weight[s1[j] - 'A'] = 1;
					else if (weight[s1[j] - 'A'] != 1) {//之前称过,而且不是重的一方
						coins[s1[j] - 'A'] = 1;
					}
				}
				for (int j = 0; j < s2.size(); j++) {
					if (coins[s2[j] - 'A'] == 2)coins[s2[j] - 'A'] = 0, weight[s2[j] - 'A'] = -1;
					else if (weight[s2[j] - 'A'] != -1) {
						coins[s2[j] - 'A'] = 1;
					}
				}
				for (int j = 0; j < 12; j++) {
					int a = s1.find(j + 'A'), b = s2.find(j + 'A');
					if (a == -1 && b == -1)
						coins[j] = 1;//不在假的里面就是真的
				}
			}
			else {//左边轻
				for (int j = 0; j < s1.size(); j++)
				{//不是真的就可能是假的
					if (coins[s1[j] - 'A'] == 2)coins[s1[j] - 'A'] = 0, weight[s1[j] - 'A'] = -1;
					else if (weight[s1[j] - 'A'] != -1) {
						coins[s1[j] - 'A'] = 1;
					}
				}
				for (int j = 0; j < s2.size(); j++) {
					if (coins[s2[j] - 'A'] == 2)coins[s2[j] - 'A'] = 0, weight[s2[j] - 'A'] = 1;
					else if (weight[s2[j] - 'A'] != 1) coins[s2[j] - 'A'] = 1;
				}
				for (int j = 0; j < 12; j++) {
					int a = s1.find(j + 'A'), b = s2.find(j + 'A');
					if (a == -1 && b == -1)
						coins[j] = 1;//不在假的里面就是真的
				}
			}
		}
		for (int i = 0; i < 12; i++) {
			if (coins[i] == 0) {
				char s = i + 'A';
				if (weight[i] == 1) s1.assign("heavy");
				else s1.assign("light");
				cout << s << " is the counterfeit coin and it is " << s1 << "." << endl; break;
			}
		}
	}
}
Published 186 original articles · won praise 13 · views 9308

Guess you like

Origin blog.csdn.net/csyifanZhang/article/details/105214518
Recommended