Reprinted please indicate the source: Thank you
Http://blog.csdn.net/lyy289065406/article/details/6661421
General question:
There is a dozen (12) coins, including only one counterfeit coin and 11 real coins.
Use ~ L as the code of each coin
Counterfeit Coins may be slightly lighter or heavier than real coins
Currently, the daily metric is used to determine whether the counterfeit currency is light or heavy based on the input three times.
Solution:
The simulation method is cumbersome and can be used for solving problems through simple logical reasoning.
Note that one input line indicates a weighing operation. Each line has three strings, which are
Left right status
Indicates the coins placed on the left disk of the day during the weighing, the coins placed on the right disk of the dayRight DiskStatus
There are three statuses:
Up: if the right disk is increased, it indicates that the right disk may have fake coins or duplicate coins.
Down: The right disk may fall, indicating that the right disk may have a duplicate counterfeit currency, or the left disk may have a lighter counterfeit currency.
Even: balance between the right disk and the left disk. Because there is only one counterfeit coin, the coins on both sides of the day are real coins.
Note:
1. There is only one counterfeit coin
2. The weight of a counterfeit currency relative to a real coin may be light or heavy.
3. only weigh 3 times, and weigh 3 times exactly and you must be able to find the counterfeit currency
4. The number of coins on both sides is the same for every weighing.
5. Input determines which coins are used for weighing.
As we can see from 3, 4, and 5, since we cannot know the coins we choose to weigh each time, we may choose only a few coins for three weighing times, or only one or two coins are not selected, it is very difficult to use the simulation method to record the status of each coin used for weighing (true and false, where the counterfeit coin has weight points) and derive the status of the coin that has not been weighed (or the status changes, although it is easy for people to do this, it is difficult for computers to "deduce" because there are many and irregular ways to weigh coins.
You can only solve the problem by converting the problem appropriately and then using another effective method.
Although the coin weighing method is irregular and unknown, there are only three results after the coin is weighed: Up, down, and even. And when even occurs, the coins on both sides of the day must be real coins, and the counterfeit coins must be between the remaining coins (this is because there is only one counterfeit coin ), then we can define a tag array zero [] to mark the real currency when even, and exclude them in future processing.
The only thing that is hard to handle is the up and down statuses. Because the counterfeit currency may be heavy or light, neither of these statuses can determine which side of the counterfeit currency appears in the day.
Methods for handling the up and down statuses:
When up or down occurs, all coins on both sides of the day should be suspected as counterfeit coins (Coins marked as real coins do not need to be suspected ).
First, time [] records the extent to which each coin is suspected. Time [I] = 0 indicates that the coin I is not suspected (that is, it can be a real coin ). The coin defined in the up status disk is "slightly suspected counterfeit currency", and the "--" operation is used to deepen the suspected degree of being a fake coin, and the "negative" is the direction of suspicion of the fake coin; the coin in the down status disk is a "suspected counterfeit currency", and the "++" operation is used to deepen the suspected counterfeit currency. The "positive" is the direction of the suspected counterfeit currency.
If a real coin is suspected to be a "fake coin", it may be removed by the "++" operation next time. The suspicion of initializing all coins is 0.
After weighing, find the coin with the highest degree of suspicion (pay attention to the absolute value). It is a counterfeit coin. When the direction of the suspect is positive, it is a counterfeit currency. When it is negative, it is a fake coin.
// Memory time // 252 K 0 Ms # include <iostream> # include <cmath> using namespace STD; int main (void) {INT cases; CIN> cases; for (INT c = 1; C <= cases; C ++) {char left [3] [6], right [3] [6], status [3] [6]; int time ['l' + 1] = {0 }; // mark the number of times each letter is suspected bool zero ['l' + 1] = {false}; // The letter that marks the absolute real currency (all letters that balance the day) for (int K = 0; k <3; k ++) CIN> left [k]> right [k]> status [k]; for (INT I = 0; I <3; I ++) {Switch (status [I] [0]) // check the daily running status {Case 'U ': // up, daily renewal left heavy right light {for (Int J = 0; left [I] [J]; j ++) {time [left [I] [J] ++; // left-heavy time [right [I] [J] --; // right-light} break ;} case 'D': // down, daily values: Left light and right heavy {for (Int J = 0; left [I] [J]; j ++) {time [left [I] [J] --; // left light time [right [I] [J] ++; // right heavy} break ;} case 'E': // down, daily round balance {for (Int J = 0; left [I] [J]; j ++) {zero [left [I] [J] = true; // absolute real coin zero [right [I] [J] = true; // absolute real coin} break ;}} int max =-1; // find the most suspected coin (counterfeit coin) Char Alpha; For (Int J = 'a '; j <= 'l'; j ++) {If (zero [J]) // absolute real currency continue; If (max <= ABS (time [J]) {max = ABS (time [J]); alpha = J ;}} cout <Alpha <"is the counterfeit coin and it is "; if (time [Alpha]> 0) cout <"heavy. "<Endl; elsecout <" light. "<Endl;} return 0 ;}