Use C language to predict the result of the game --- cultivate programming thinking

        Hello everyone, I am Boom Jiabao in c language. What I bring to you today is a very interesting thing. It is also to cultivate the programming thinking of beginners, and use the programming language learned to solve some small problems in life. Now look at the topic:

5 athletes participated in the 10-meter platform diving competition, and someone asked them to predict the result of the competition:

Player A said: B is second, I am third;

Player B said: I am second, E is fourth;

Player C said: I am first, D is second;

Player D said: C is last, I am third;

Player E said: I am fourth, A is first;

After the game, each player is half right, please program to determine the ranking of the game .

        I don’t know if the friends have any ideas when they see this question. The blogger will briefly share his ideas here, hoping to inspire those students who have not been exposed to programming for a long time to integrate programming into their lives as soon as possible and cultivate programming thinking.

        According to the topic, there are five contestants in total, so we first define five variables and assign the initial value to 0;

        Then, if everyone goes from the 1st to the 5th, there will be a total of 5^5 possibilities. This only needs a 5-layer cycle to get it done.

        

        The title says that half of what each player said is correct, so how do we define the truth and lies that he said separately. In fact, it is natural to think that there are also judgments of true and false in the computer, that is, 0 is false and non-zero positive integers are true, and of course half of them default to 1 as true. Therefore, if each player says a lie, the value is 0; if there is another truth, the value is 1. The value of these two sentences added together is 1. Then because we put him in the non-stop traversal, we can finally achieve the effect of predicting the result of the game. The code is as follows:

 

         Let's see the compilation result

: 

        

        We can see that the computer has given many answers. This is because we have not considered that there will be the same ranking. For this problem, we can add an if statement to judge, for example, let a not equal to b and b not equal to c.. … It can also be simpler, if the rankings are different, that is, the rankings are 1, 2, 3, 4, and 5 in sequence, and the number of flights is 1*2*3*4*5=120. code show as below:

This way we can achieve our goal! Friends can first use the computer to help calculate the answer, and analyze the above questions with a smart brain to verify whether the computer is doing it right hahahaha.

 

 

        

Guess you like

Origin blog.csdn.net/m0_73321818/article/details/131293602