/* Eight silver coins in eight pieces of the same appearance of the coin, one is a counterfeit currency, and it is known that the difference in the weight of the real currency, 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. Detects the location of the fake coin and whether the coin is light or heavy input: The first line is the weight of the eight coins, separated by a space output: The second line of the output of counterfeit money in the position of the eight coins the third line of the output of counterfeit money biased or light example input: 6 6 6 6 6 9 6 6 6 6 Sample output: 6 Heavier problem analysis: eight coins (a,b,c,d,e,f,g,h) because eight coins in 1 coins are different from the other 7. Therefore, eight pieces can be divided into three groups: 1, a,b,c 2, d,e,f 3, g,h process (can be used to compare two groups of coins by a balance); I, judging (a,b,c) and (d,e,f) weight (1) (a,b,c) = = (d,e,f) * That is, the counterfeit currency must be g or H * First to determine if (g > H), and then determine whether g (or h) is equal to (a,b,c,d,e,f) One: If H (or g) is a counterfeit currency and outputs the result lighter. If otherwise h (or g) is counterfeit, and the output result is heavier. (2) (A,b,c) > (d,e,f) * That is, counterfeit money must be a or B or C or D or E or F * re-group to (A,e) (b,d) (c,f): The position of the exchange b,e can be determined ( A,D) for false or (B,E) is false. Determine if (A+e > D+b) symbol is still >: Note that counterfeit money must be (a,d) an if (a+e = = d+b) Note that counterfeit money must be (c,f) one of if (A+e < d+b) symbol change: Note that the counterfeit currency must be (b,e) one of (3) (A,b,c) < (D,E,F) * That is, the counterfeit currency must be a or B or C or D or E or F * determine if (A+e < d+b) symbol is still <: Note that the counterfeit currency must be (a,d) an if (a+e = = d+b) Note that the counterfeit currency must be (c,f) an if (A+e > D+b) symbol change: Note that the counterfeit currency must be (b,e) a II, thus, declaring 3 functions respectively processing ①eightcoins used to differentiate possible counterfeit currency groups (ELEM1,ELEM2) ②compare used for the separation of counterfeit currency (sham) ③print for printing Results III, code implementation as follows ... */#include <iostream>using namespace Std;voidEightcoins (int* arr);//Judging True and falsevoidPrint (intShamint Real,int Index);//OutputvoidCompare (intELEM1,intELEM2,int Real,intIndex1,intINDEX2);//Judging severityintMain () {intIintarr[8]; for(i=0; i<8; i++) cin>>arr[i]; Eightcoins (arr);return 0;}//Judging severityvoidEightcoins (int* arr) {intA = arr[0], B = arr[1], C = arr[2], d = arr[3], E = arr[4], F = arr[5], G = arr[6], H = arr[7];intABC = a+b+c, Def = d+e+f;if(ABC > Def) {if((A+e) > (d+b))//Remove c,f and swap b,e position still > Description One of the a,d must be counterfeitCompare (A,d,g,0,3);Else if((a+e) = = (d+b))//Remove c,f (a+b) = = (D+e) Note that one of the c,f must be counterfeitCompare (C,f,g,2,5);Else //Remove c,f and swap b,e position symbol change Description b,e must have a counterfeit currencyCompare (B,e,g,1,4); }Else if(abc = = def) Compare (G,h,a,6,7);//The best situation Else{if((A+e) < (D+B))//Remove c,f and swap b,e position still > Description One of the a,d must be counterfeitCompare (A,d,g,0,3);Else if((a+e) = = (d+b))//Remove c,f (a+b) = = (D+e) Note that one of the c,f must be counterfeitCompare (C,f,g,2,5);Else //Remove c,f and swap b,e position symbol change Description b,e must have a counterfeit currencyCompare (B,e,g,1,4); } }//Compare true and FalsevoidCompare (intELEM1,intELEM2,int Real,intIndex1,intINDEX2) {if(Elem1 = =Real) Print (ELEM2,Real, INDEX2);ElsePrint (ELEM1,Real, index1);}//OutputvoidPrint (intShamint Real,int Index){if(Sham >Real) cout<<Index+1<<endl<<"Heavier"<<endl;Elsecout<<Index+1<<endl<<"Lighter"<<endl;}
Exercise (13): eight coin