N Coin problem (find counterfeit money)

Source: Internet
Author: User

Problem Description:

In the same appearance of the n coins, one is a counterfeit currency, and it is known that the weight of counterfeit money and real currency is different, but do not know whether the counterfeit currency is relatively lighter or heavier than the real coin. A balance can be used to compare two groups of coins arbitrarily, designing an efficient algorithm to detect this counterfeit currency.

Problem Solving Ideas:

Using the method of solving the problem of reducing the number of coins divided into 3 piles, the amount of coins per heap is N/3, but this is in the case of n%3==0 can be set up, so we will be n coins divided into 3 heap plus 1 heap remainder heap (the remainder heap may be 0), then can be divided into the following (n-n%3)/3, (n-n%3)/3, n%3.

Such as:

(n-n%3)/3 (n-n%3)/3 (n-n%3)/3N%3ABCD
    1. First get the real currency, through a random array of three coins, compare each other, the equivalent of two for the real coin, arbitrarily take one as a real currency array subscript.
    2. First determine the number of coins in N, if N>2 is executed 2, otherwise execute 5.
    3. Divide n into four piles, compare A and B, if a = = B, then counterfeit money is in C or D. Otherwise the counterfeit currency is in a or B.
    4. If a = = B, compare A and C. If a = = C, then the counterfeit currency is in D. Execute this process again with D. If not, then the counterfeit currency is in C, and the D ( remainder heap ) is executed again, and n=n%3.
    5. If a! = B, then compare A and C. If a = = C, the counterfeit currency is in B. Execute the process again with B. If not, then the counterfeit currency is in a, a process 2 is executed again, and n= (n-n%3)/3.
    6. If n==2, then the two coins and real coins (through the array subscript) to compare, different for counterfeit money, output, the end.
    7. If n==1, then the coin is counterfeit, the output ends.

  * Note: According to the 2-5 process of the heap down, in the last execution Process 2 o'clock, N contains counterfeit money, and n can only be 1 or 2. (Initial, n>3, if n<3, it can not be judged true or false)

The main code is as follows:

1 //calculate the total weight of a coin2  intSum_coin (intCoin[],intMintN) {3      intresult=0;4      if(m>N)5          return 0;6       for(inti=m;i<=n;i++){7result+=Coin[i];8      }9 Ten      returnresult; One  }; A  -  -  //find counterfeit m, n array subscript, coin coin array, relcoin real currency array subscript the  intCheck_coin (intCoin[],intMintNint&relcoin) { -  -         intvary=n-m+1; -  +         intrestcoin=vary%3; -         intvary2=vary-Restcoin; +  A         if(vary==1) at             returnm; -  -         if(vary==2) -         { -             if(Sum_coin (coin,m,m) = =Sum_coin (coin,relcoin,relcoin)) -                 returnN; in             Else -                 returnm; to  +         } -  the  *         if(Sum_coin (coin,m,m+vary2/3-1) ==sum_coin (coin,m+vary2/3, m+ (vary2/3)*2-1))//First pile = = Second Pile $         {Panax Notoginseng             if((Sum_coin (coin,m,m+vary2/3-1) ==sum_coin (coin,m+ (vary2/3)*2, m+vary2-1)))//First pile = = Third Pile -Check_coin (coin,n-restcoin+1, n,relcoin); the             Else//The First bunch! = Third Heap +Check_coin (coin,m+ (vary2/3)*2, m+vary2-1, relcoin); A         } the         Else//The First bunch! = Second Pile +         { -             if(Sum_coin (coin,m,m+vary2/3-1) ==sum_coin (coin,m+ (vary2/3)*2, m+vary2-1))//First pile = = Third Pile $Check_coin (coin,m+vary2/3, m+ (vary2/3)*2-1, relcoin); $             Else//The First bunch! = Third Heap -Check_coin (coin,m,m+vary2/3-1, relcoin); -         } the  -  };Wuyi  the  -  //returns the real currency array subscript Wu  intGetrelcoin (intCoin[],intMintN) -  { About      if(n-m+1<=2) $      { -cout<<"The number of coins is less than 3 pieces!!! No solution"; -         return-1; -      } A      Else +      { the          if(coin[0]==coin[1]) -          { $              return 0; the          } the          Else the          { the              if(coin[0]==coin[2]) -                  return 2; in              Else the              { the                  return 1; About              } the          } the      } the  +};

N Coin problem (find counterfeit money)

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.