Turn: 1 ~ 40 pounds of 4 weights

xiaoxiao2021-03-06  34

French mathematician Melzziac proposed a question in his famous "digital combination game" (1962): a businessman has a weight 40 pound weight, and accidentally falls the weight into four. Later, businessmen weighted the weight of each piece, and found that the four pieces of debris can be called any weight between 1 to 40 pounds on the balance. How much is these four pieces of debris?

* Question analysis and algorithm design

The conditions given in the topic are "on the balance", which means that the same weight can be placed on the left side of the balance, or it can be placed on the right side of the balance. If the specified weight can only be placed on the left side of the balance, there is a flat balance on the day:

Heavy weight left weight sum total = right weight weight sum

Therefore:

Heavy weight = right weight weight sum - the weight of the left weight weight

When programming, as long as the above formula is based on the above formula, "the weight of the weight of the right side", the weight of the left side can represent all weights between 1 and 40. It should be noted in the program: how to use a simple method to indicate that a weight is on the left side of the balance or on the right side of the balance, or is not used at all.

The following procedures indicate the above three cases, respectively, please note.

* Program and Program Note #include #include void main () {int Weight1, weight2, weight3, weight4, d1, d2, d3, d4, x, flag; / * flag: satisfaction The meaning of the title * / printf ("THE Weight IS Broke Up as Following 4 Pieces:"); for (Weight1 = 1; Weight1 <= 40; Weight1 ) / * Dissure 40 to 4 * / for (Weight2 = Weight1 1; weight2 <= 40-weight1; weight2 ) for (weight3 = weight2 1; weight3 <= 40-weight1-weight2; weight3 ) if ((weight4 = 40-weight1-weight2-weight3)> = weight3) {for (FLAG = 1, x = 1; x <41 && flag; x ) / * determines whether all weight * / for (FLAG = 0, D1 = 1; D1> -2; D1 -) / * Place the heavy object in the left of the balance * / for (D2 = 1; D2> -2 &&! Flag; D2--) / * 1: Weight in the right side * / for (D3 = 1; D3> -2 && Flag; D3 -) / * 0: No need to this weight * / for (D4 = 1; D4> -2 &&! flag; d4--) / * - 1: Weight on the left of the balance * / if (x == Weight1 * D1 Weight2 * D2 Weight3 * D3 Weight4 * D4) FLAG = 1; if (flag) Printf ("% D% D% D% D / N", weight1, weight2, weight3, weightt4); Flag = 0;}} * Operations The weight is Broke Up as Following 4 PIECES: 1 3 9 27

转载请注明原文地址:https://www.9cbs.com/read-63790.html

New Post(0)