Certainly! Below is a C++ program that solves the problem of identifying the counterfeit coin among 12 coins based on three weighings. The program systematically tests each possible counterfeit coin (assuming it could be either heavier or lighter) and checks if it aligns with the results of all three weighings. Once it finds a match, it outputs the counterfeit coin along with whether it's heavier or lighter.
C++ Solution:
#include <bits/stdc++.h>
using namespace std;
struct Weighing {
string left;
string right;
string result;
};
// Function to simulate a weighing based on the assumption
// fake_coin: the assumed counterfeit coin
// is_heavy: true if assumed to be heavier, false if lighter
// weigh: the weighing to simulate
int simulateWeighing(char fake_coin, bool is_heavy, const Weighing &weigh) {
// Assign weights
unordered_map<char, int> weights;
// All genuine coins have weight 1
for(char c = 'A'; c <= 'L'; c++) {
weights[c] = 1;
}
// Adjust the fake coin's weight
if(is_heavy)
weights[fake_coin] = 2; // heavier
else
weights[fake_coin] = 0; // lighter
// Calculate total weight of left and right pans
int left_sum = 0, right_sum = 0;
for(char c : weigh.left)
left_sum += weights[c];
for(char c : weigh.right)
right_sum += weights[c];
if(left_sum > right_sum)
return 1; // left heavy
else if(left_sum < right_sum)
return -1; // left light
else
return 0; // balance
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// Read input until no more test cases
while(true){
Weighing w[3];
// Read three lines for a test case
bool eof = false;
for(int i=0;i<3;i++){
if(!(cin >> w[i].left >> w[i].right >> w[i].result)){
eof = true;
break;
}
}
if(eof)
break;
// Try all possible fake coins
bool found = false;
char fake_coin;
string fake_status;
for(char c = 'A'; c <= 'L'; c++){
for(bool is_heavy : {true, false}){
bool valid = true;
for(int i=0;i<3;i++){
int sim = simulateWeighing(c, is_heavy, w[i]);
// Compare sim with actual result
string actual = w[i].result;
if(sim == 1 && actual != "nang")
{valid = false; break;}
if(sim == -1 && actual != "nhe")
{valid = false; break;}
if(sim == 0 && actual != "bang")
{valid = false; break;}
}
if(valid){
fake_coin = c;
if(is_heavy)
fake_status = "nang hon";
else
fake_status = "nhe hon";
found = true;
break;
}
}
if(found)
break;
}
// Output the result
cout << fake_coin << ", " << fake_status << "\n";
}
}
Explanation:
-
Struct Definition:
Weighing: A structure to store each weighing's left pan coins, right pan coins, and the result.
-
simulateWeighing Function:
- Purpose: Simulates the outcome of a weighing based on an assumed counterfeit coin and whether it's heavier or lighter.
- Parameters:
fake_coin: The coin assumed to be counterfeit.
is_heavy: A boolean indicating if the fake coin is heavier (true) or lighter (false).
weigh: The weighing details (left, right, result).
- Process:
- Assigns a default weight of
1 to all genuine coins.
- Adjusts the weight of the assumed fake coin:
2 if heavier, 0 if lighter.
- Calculates the total weight of the left and right pans.
- Determines the simulated result (
1 for left heavy, -1 for left light, 0 for balance).
-
Main Function:
- Input Handling:
- Continuously reads three weighings per test case until the end of the input.
- Logic:
- Iterates through all possible coins (
A to L) and both possibilities (heavier and lighter).
- For each assumption, it simulates all three weighings.
- If the simulated results match the actual results for all three weighings, it identifies the counterfeit coin and its nature (heavier or lighter).
- Output:
- Once the counterfeit coin and its status are identified, it outputs them in the required format (e.g.,
K, nhe hon).
Sample Input:
ABCD EFGH bang
ABCI EFJK nang
ABIJ EFGH bang
Sample Output:
K, nhe hon
How to Use:
-
Compilation:
- Save the code in a file named
XUGIA.cpp.
- Compile using a C++ compiler, for example:
g++ -o XUGIA XUGIA.cpp
-
Execution:
- Run the compiled program and provide the input as per the problem statement.
- Example:
./XUGIA
- Then input the weighings, each on a new line.
-
Input Termination:
- The program reads until the end of the input (EOF). Ensure that all test cases are provided accordingly.
This solution ensures that all possibilities are checked systematically, guaranteeing the correct identification of the counterfeit coin based on the provided weighings.