Coin
InterviewTime limit1sMemory limit128 MB
Determine the fake coin among 12 and whether it is heavier or lighter, given three balance weighing results, or report impossible/indefinite.
- Level
Medium5 of 10
- Topics
- Brute force, Simulation, Implementation
- Solved
- No attempts yet
Problem
You have a two-pan balance scale and 12 coins numbered 1 through 12. Exactly one of them is a counterfeit, which is either heavier or lighter than a genuine coin. All genuine coins weigh the same.
Given the results of three weighings on the balance scale, write a program that determines which coin is the counterfeit and whether it is heavier or lighter than a genuine coin.
Input
The input consists of three lines. Each line describes one weighing in the following format:
A B C D x E F G H
A, B, C, D, E, F, G, H are the numbers of eight distinct coins, and x is one of <, >, =, with the following meaning:
<: the total weight of A, B, C, D is less than that of E, F, G, H>: the total weight of A, B, C, D is greater than that of E, F, G, H=: the total weight of A, B, C, D equals that of E, F, G, H
Output
If the counterfeit can be uniquely identified, print its number followed by + if it is heavier than a genuine coin, or - if it is lighter (for example, 2+).
If the three weighings contradict each other so that no coin can satisfy them, print impossible.
If the weighings are not contradictory but the counterfeit's number, or whether it is heavier or lighter, cannot be uniquely determined, print indefinite.