Problem link: https://onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=549 Problem statement:

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 knowif 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 ofthe 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 orlight. The solution will always be uniquely determined.

Explanation:

It's a tricky problem, but the solution is pretty trivial, we just simulate the process and the logic.For that we must come with an assumption for each coin given,if it's either heavy or light, suposse the coin is light and it's found in the left side first time then the left side is lighter and it must be up, in the second case it's in the right side so the ride side is up.It's a simple logical assumption that should be true for all the 3 cases if it's false at some point then the coin is normal and if the coin is found in a even case,it is again a normal coin, but if it goes through all the tests it is indeed conterfeit.

My code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>


using namespace std;

string measure[3][3];
int EVEN = 1,
    LIGHT = -50,
    HEAVY = 50;

bool isBalanced(char ch,bool isHeavy){
	int leftWeight,rightWeight;
	for(int i = 0;i<3;i++){
		leftWeight = rightWeight = 0;
	    for(const char k: measure[i][0]){
			if(k==ch)leftWeight += (isHeavy?HEAVY:LIGHT);
			}
		for(const char k:measure[i][1]){
			if(k==ch)rightWeight+=(isHeavy?HEAVY:LIGHT);
			}
		if(measure[i][2]=="even"){
			if(leftWeight!=rightWeight)return false;
			}
		else if(measure[i][2]=="up"){
			if(leftWeight<=rightWeight)return false;
			}
		else{
			if(leftWeight>=rightWeight)return false;
			}
	}
	return true;
}

int main(int argc, char **argv)
{
	for(int i = 0;i<3;i++){
		for(int j=0;j<3;j++){
			cin>>measure[i][j];
			}
		}
	for(char c = 'A';c<='L';c++){
		if(isBalanced(c,true)){
			printf("Coin %c is heavier\n",c);
			}
		if(isBalanced(c,false)){
			printf("Coin %c is lighter\n",c);
			}
		}
		return 0;

	}