Coin Title Summary

Source: Internet
Author: User

Questions like this:

There are eight coins, one of which is fake, and the amount is measured on the balance. I don't know whether the fake is really heavy or light. Could you tell me how many times it will take to determine whether it is false, severity (5 points ). Use C code (25 points ).

First, determine the number of times

8 balls * 2 status = 16
Balance 3 (left and right)
Known 3 ^ 2 <16 <3 ^ 3
So it can be called at least three times.

Then, find out the bad one. The simplest method is to determine whether it is light or heavy, and then locate it by 3 points. however, there is a problem here, and there is no way to determine whether the problem is light or heavy without the benchmark value. you can only move this step to the next step.

Construct two ing tables

0
1 2
3 4 5
6 7  

First measurement [012] [345]

Second measurement [036] [147]

If the problematic one is heavy, the problematic ID can be obtained based on the two detection results.

  Left Right Ping
Left 0 1 2
Right 3 4 5
Ping 6 7  

Similarly, if there is a problem, it is light.

  Left Right Ping
Left 4 3 5
Right 1 0 2
Ping 7 6  

Then it's easy to merge two tables. In this way, we can find two coins, one of which is bad, and the other six are normal.

For the third time, check whether one of the coins is equal to a normal coin,

So we can find out.

Coin find_bad_coin (COIN coins [8]) {int tab [3] [3] ={{, 25 },{, 52 },{,-1 }}; // test () = 0 left weight, test () = 1 right weight, test () = 2 flat int T1 = test (Coins [0] + coins [1] + coins [2], coins [3] + coins [4] + coins [5]); int t2 = test (Coins [0] + coins [3] + coins [6], coins [1] + coins [4] + coins [7]); int res = tab [T1] [T2]; // obtain a normal coin int cc = t1 = 2? 0: 6; If (test (Coins [res % 10], coins [CC]) = 2) return coins [Res/10]; else return coins [res % 10];}

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.