POJ1013 Counterfeit Dollar solution report

Source: Internet
Author: User

There are 12 coins (marked as A-L), one of which is a counterfeit coin, but I do not know whether the counterfeit coin is lighter or heavier than the real coin. With the help of the balance, Sally designed a solution that could just three times measure who is a fake coin, lighter or heavier than a real coin. Ask you to help Sally. Based on her measurement data, calculate who is a fake coin, lighter or heavier than a real coin. For example, a group of measurement data is: abcd efgh even abci efjk up abij efgh even note: the number of coins around the balance is always equal. It can be calculated that K is a counterfeit currency and is lighter than a real coin. The key to solving this problem is to grasp the following key points: (1) there is only one counterfeit currency. Therefore, in the unbalanced measurement, the counterfeit currency will certainly appear. Recording the number of times coins appear in the unbalanced measurement is a condition for determining counterfeit coins. However, using this condition alone is not enough. For example, in this example, a B up cd AB up c d even, the number of times A and B appear in the unbalanced measurement is equal, both of which are 2, therefore, we need to consider the second point. (2) The counterfeit currency is consistent in measurement. If it is lighter than the real currency, it must always be on the up side in the imbalance measurement. Therefore, in the unbalanced measurement, sometimes on the up side, and sometimes on the down side, the in the above example shows that it has no decisive effect on the unbalanced measurement, it must be a real coin and should be marked out. (3) the coins that appear in the balance measurement are all real coins. mark them. The algorithm IDEA defines the COIN structure as follows: struc COIN {bool appear; int type; int count ;}; where, whether the appear mark appears in three measurements, because not all coins are involved in measurement, type indicates the type of coins. Initially, it is UNDEFINED. If it is determined to be a real coin, it is DOLLAR. If it is not a real coin, it is LIGHT; otherwise, it is HEAVY; count indicates the number of times coins appear in the imbalance measurement. The algorithm is divided into three steps: Step 1: Initialize the COIN array to prepare for this round of computing. Step 2: update the COIN Array Based on the input measurement data. Step 3: scan the COIN array, find the counterfeit currency, and print its information. Code // 1013.cpp // 2012-12-27 by btwsmile # include <stdio. h> # include <string. h> // define # define UNDEFINED-1 # define LIGHT 0 # define DOLLAR 1 # define HEAVY 2 # define N 12 // struct COIN {bool appear; int type; int count;}; // functions // prepare-reset coin [] void prepare (COIN *); // update-update coin [] void update (COIN *, char *, char *, char *); // search-find Counterfeit in coi N [] int search (COIN *); // entry int main () {COIN [N]; char szLeft [10]; char szRight [10]; char szRelation [10]; int n; scanf ("% d", & n); for (int I = 0; I <n; ++ I) {prepare (coin); // scanf & update for (int j = 0; j <3; ++ j) {scanf ("% s", szLeft, szRight, szRelation); update (coin, szLeft, szRight, szRelation);} // find Counterfeit int iCounterfeit = search (coin); // print if (iCounterfeit >-1) {printf ("% c is the counterfeit coin and it is % s. \ n ", ('A' + iCounterfeit), (coin [iCounterfeit]. type = HEAVY )? "Heavy": "light") ;}} return 0 ;}// definitions // prepare void prepare (COIN * coin) {for (int I = 0; I <N; ++ I) {coin [I]. appear = false; coin [I]. type = UNDEFINED; coin [I]. count = 0 ;}// update void update (COIN * coin, char * pszLeft, char * pszRight, char * pszRelation) {int iSize = strlen (pszLeft ); char * psz [2] = {pszLeft, pszRight}; if (strcmp (pszRelation, "even") = 0) // "even" {(Int I = 0; I <iSize; ++ I) {for (int j = 0; j <2; ++ j) {int index = psz [j] [I]-'A'; coin [index]. appear = true; coin [index]. type = DOLLAR ;}} else // "up" or "down" {int iTypeLeft, iTypeRight; if (strcmp (pszRelation, "up") = 0) {iTypeLeft = HEAVY; iTypeRight = LIGHT;} else {iTypeLeft = LIGHT; iTypeRight = HEAVY;} for (int I = 0; I <iSize; ++ I) {for (int j = 0; j <2; ++ j) {int index = Psz [j] [I]-'A'; coin [index]. appear = true; coin [index]. count ++; if (coin [index]. type! = DOLLAR) {int iType = (j> 0 )? ITypeRight: iTypeLeft); if (coin [index]. type = UNDEFINED) coin [index]. type = iType; else if (coin [index]. type! = IType) coin [index]. type = DOLLAR ;}}// end else} // search int search (COIN * coin) {int iMaxCount = 0; int iCounterfeit =-1; for (int j = 0; j <N; ++ j) {if (coin [j]. appear & coin [j]. type! = DOLLAR & coin [j]. count> iMaxCount) {iMaxCount = coin [j]. count; iCounterfeit = j ;}} return iCounterfeit ;}

Related Article

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.