HDU 1482 counterfeit dollar

Source: Internet
Author: User
Tags first string
Counterfeit dollar

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 274 accepted submission (s): 117

Problem descriptionsally 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 know if 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.

 

Inputthe 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 of the balance goes up, down, or remains even.

Outputfor each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.

Sample Input

1ABCD EFGH evenABCI EFJK upABIJ EFGH even
 

Sample output

K is the counterfeit coin and it is light.
 

Sourceeast central North America 1998

Recommendxhd

One of the 12 silver coins is fake and shoddy. You need to find it. The question will be weighed three times (to ensure the number of silver coins on both sides of the same) the status of the left side of the balance.

You need to find out which fake and shoddy silver coins are based on the given data. Whether the weight is light or heavy.

We assume that the false information is heavy.

It can be proved that

①. A counterfeit silver coin is always down.

2. If a silver coin appears both in the down and in the up, it must be true,

3. If the balance is in the even state, the silver coins on both disks are true. (Because there is only one fake coin)
The balance has only three States. One Weighing Balance, Two Weighing balances. And zero weighing.

Three-way balance is impossible (if three-way balance, although it may find counterfeit currency, it cannot be determined)

That is to say, if the balance is zero, the coin that goes down three times is heavy. (The problem must be solved. Therefore, other dwons' three times can be excluded through the previous measurement or subsequent measurement)

If you go down three times on the same side, you cannot determine who is false and does not satisfy the uniqueness. Downloading across different sides three times. Assuming that A and B are down three times and A is not sure whether it is true, it is equivalent to starting to put a and B on both ends of the balance. And then add other loads at both ends. Of course, you cannot determine who is false. If you go down twice, the same side is down twice. If you go down twice, you can only have one unknown, true, or false (unique). If you go down twice, the same side is down three times.

It is the easiest to go down once. There must be only one uncertain (through the theorem given above.

The code is just a rough description. A little long. Time-based optimization.

# Include <stdio. h> # include <string. h> int ABS (int x) {return x <0? -X: X;} int main () {int OK [13], N, P, I, j, ANS, mi; // OK records the true and false coins. // four statuses are available: 4. positive, negative, and 0. 4. The unknown positive indicates the number of times the coins are weighed down. the number of times when the negative value is up // 0 is determined as the true silver coin char L [10], R [10], W [10]; // L records the coin on the left. R records the silver coins on the right disk. W record the balance status scanf ("% d", & N); getchar (); While (n --) {for (I = 0; I <= 12; I ++) // Initialization is unknown OK [I] = 4; for (I = 0; I <3; I ++) {scanf ("% S % s", l, r, W); If (! Strcmp (W, "up") // If the left side is light {for (j = 0; L [J]! = '\ 0'; j ++) {P = L [J]-'A'; // map to the OK location if (OK [p] = 4) OK [p] = 1; else if (OK [p] <0) OK [p] = 0; else if (OK [p]> 0) OK [p] ++;} For (j = 0; R [J]! = '\ 0'; j ++) {P = R [J]-'A'; If (OK [p] = 4) OK [p] =-1; else if (OK [p]> 0) OK [p] = 0; else if (OK [p] <0) OK [p] -- ;}} else if (! Strcmp (W, "even") {for (j = 0; L [J]! = '\ 0'; j ++) {P = L [J]-'A'; OK [p] = 0 ;}for (j = 0; R [J]! = '\ 0'; j ++) {P = R [J]-'A'; OK [p] = 0 ;}} else {for (j = 0; L [J]! = '\ 0'; j ++) {P = L [J]-'A'; If (OK [p] = 4) OK [p] =-1; else if (OK [p]> 0) OK [p] = 0; else if (OK [p] <0) OK [p] --;} For (j = 0; R [J]! = '\ 0'; j ++) {P = R [J]-'A'; If (OK [p] = 4) OK [p] = 1; else if (OK [p] <0) OK [p] = 0; else if (OK [p]> 0) OK [p] ++ ;}}} mi = 0; For (j = 0; j <= 12; j ++) if (OK [J]! = 4 & ABS (OK [J])> mi) MI = ABS (OK [J]), ANS = J; If (OK [ANS]> 0) printf ("% C is the counterfeit coin and it is heavy. \ n ", 'A' + ans); else printf (" % C is the counterfeit coin and it is light. \ n ", 'A' + ans);} return 0;}/* 1AB CD upec BD upae cd up */

The optimized code becomes shorter, but it seems to be a little more time-consuming.

# Include <stdio. h> # include <string. h> int OK [13]; // OK records four statuses: positive, negative, and 0. 4 indicates the number of times the coin is weighed down. number of times when the negative value is up // 0 is determined to be true silver coin int ABS (int x) {return x <0? -X: X;} void solve (char * l, char * r) // solves the case where the left weight and right weight are light {Int J, P; For (j = 0; L [J]! = '\ 0'; j ++) {P = L [J]-'A'; // map to the OK location if (OK [p] = 4) OK [p] = 1; else if (OK [p] <0) OK [p] = 0; else if (OK [p]> 0) OK [p] ++;} For (j = 0; R [J]! = '\ 0'; j ++) {P = R [J]-'A'; If (OK [p] = 4) OK [p] =-1; else if (OK [p]> 0) OK [p] = 0; else if (OK [p] <0) OK [p] -- ;}} int main () {int N, P, I, j, ANS, mi; char L [10], R [10], W [10]; // L records the coin on the left. R records the silver coins on the right disk. W record the balance status scanf ("% d", & N); getchar (); While (n --) {for (I = 0; I <12; I ++) // Initialization is unknown OK [I] = 4; for (I = 0; I <3; I ++) {scanf ("% S % s", l, r, W); If (! Strcmp (W, "up") // if left-heavy, right-light, and right-side light solve (L, R); else if (! Strcmp (W, "even") // solves the equiweights at both ends {for (j = 0; L [J]! = '\ 0'; j ++) {P = L [J]-'A'; OK [p] = 0 ;}for (j = 0; R [J]! = '\ 0'; j ++) {P = R [J]-'A'; OK [p] = 0 ;}} else solve (R, L ); // left-light and right-heavy. After switching location, it becomes left-heavy and right-light} MI = 0; For (j = 0; j <12; j ++) if (OK [J]! = 4 & ABS (OK [J])> mi) MI = ABS (OK [J]), ANS = J; If (OK [ANS]> 0) printf ("% C is the counterfeit coin and it is heavy. \ n ", 'A' + ans); else printf (" % C is the counterfeit coin and it is light. \ n ", 'A' + ans);} return 0;}/* 2AB CD upec BD upae cd up */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.