Solving fake coin Problems

Source: Internet
Author: User

Description:

There are eight silver coins a B c d E F G H. It is known that one of them is a counterfeit coin. Its weight is different from that of the real coin, but it is lighter or heavier, how to Use the balance to determine which counterfeit currency is used at least, and learn that the counterfeit currency is lighter or heavier than the real currency.

Solution, we can use a decision tree (demo-tree) to help solve the problem by using an analysis and tree chart. In a simple situation, we compare A + B + C with D + E + F. If they are equal, the counterfeit currency must be g or H, we first compare g or H which is heavier, if G is heavier, then compare with a (A is the real currency), if G is equal to a, then G is the real currency, then H is the counterfeit currency, because H is lighter than G and G is the real currency, H is lighter than the real currency.

This idea is a direct logical reasoning method. The related code is as follows:

 

Void compare (INT coins [], int I, Int J, int K) {If (Coins [I]> coins [k]) {printf ("\ n counterfeit currency % d heavy", I + 1);} else {printf ("\ n counterfeit currency % d light", J + 1 );}} void eightcoins (INT coins []) {If (Coins [0] + coins [1] + coins [2] = coins [3] + coins [4] + coins [5]) {If (Coins [6]> coins [7]) {compare (coins, 6, 7, 0);} else {compare (coins, 7, 6, 0 );}} else if (Coins [0] + coins [1] + coins [2]> coins [3] + coins [4] + coins [5]) {If (Coins [0] + coins [3] = coins [1] + coins [4]) {compare (coins, 2, 5, 0 );} else if (Coins [0] + coins [3]> coins [1] + coins [4]) {compare (coins, 0, 4, 1 );} else if (Coins [0] + coins [3] <coins [1] + coins [4]) {compare (coins, 1, 3, 0 );}} else if (Coins [0] + coins [1] + coins [2] <coins [3] + coins [4] + coins [5]) {If (Coins [0] + coins [3] = coins [1] + coins [4]) {compare (coins, 5, 2, 0 );} else if (Coins [0] + coins [3]> coins [1] + coins [4]) {compare (coins, 3, 1, 0 );} if (Coins [0] + coins [3] <coins [1] + coins [4]) {compare (coins, 4, 0, 1 );}}}

Of course, the solution to this problem can also take advantage of the exclusive or feature: two identical numbers are different or 0 is later. In addition, using the "binary" idea can improve the efficiency a lot. Using this method, you can also extend the question: void falsecoins (INT coins [], int start, int end ). In this way, we can not only solve the fixed eight coins problem. The Code is as follows:

void myEightCoins(int coins[]){    int start,mid,end;    int sum1,sum2;    int right;    start = 0;    end   = 7;    mid   = (start+end)/2;    int i;    sum1 = sum2 = 0;    while (start < end - 1)    {        sum1 = coins[start];        for (i = start + 1; i <= mid; i++)        {            sum1 = sum1 ^ coins[i];        }        sum2 = coins[end];        for (i = end - 1; i > mid; i--)        {            sum2 = sum2 ^ coins[i];        }        if (sum1 == 0)        {                right = start;            start = mid + 1;            mid   = (start+end)/2;        }        if (sum2 == 0)        {            right = end;            end = mid;            mid = (start+end)/2;        }    }        if (coins[start] > coins[end])    {        compare(coins,start,end,right);    }    else    {        compare(coins,end,start,right);    }}

 

 

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.