Description:
The old coin problem. There is a pile of coins numbered 1-n (n <100) in sequence, with only one weight slightly different. Next, K (k <100) calls the coin operation to give the results of each weighing, that is, the weight of A1, a2... am is greater than or less than B1, B2.... Bm number. Output coin numbers with different weights.
Analysis:
1. If the two heaps of coins are of the same weight, all coins are normal coins.Coins that never appear in the case of equal weight are suspected.
2. If some coins appear on a smaller or larger side each time, this coin is suspected;Otherwise, it must be normal.
3. Combine the above two points to find all the suspected coins. If the value is 0 or greater than 1, the coin cannot be found; otherwise, the coin is output.
In fact, the idea is very clear. You only need to infer from your life experience.
In the programOptimization. I usedBitwise operationThe question is coming soon in 0.01 s.
For example, I used a 10-element int array to record which coins have appeared in the case of equal weights. Each int stores the status of 10 coins. Initially, all a [I] is~ 0That is to say, each digit is 1, which means all of them are suspected. Then use a [X/10] & = ~ (1 <(X % 10) indicates that the coin number X is no longer suspected.The X % 10 position of a [X/10] is 0..
According to this idea, the other situation can also be solved ~
The code is not pasted ...... Ugly.