Use c programming to simply speculate who is the murderer-------cultivate programming thinking

        hello everyone, I am Boom Jiabao in c language. What the blogger still brings today is an interesting topic for cultivating programming thinking, using C language to deduce who is the murderer. (The blogger’s last blog talked about how to program to realize the prediction of the game. Those who are interested can go to the homepage to watch). Known issues are as follows:

A murder case occurred in a certain place in Japan. The police determined through investigation that the murderer must be one of the four suspects.

The following are the confessions of the four suspects:

A said: Not me.

B said: It is C.

C said: Yes D.

D said: C is talking nonsense

It is known that 3 people told the truth and 1 person told the lie.

Now please write a program to determine who the murderer is based on this information.

        Given this question, we can make a simple reasoning first: only one of the four people told a lie, then assume that a told a lie, then a is the murderer, b told the truth, then the murderer is c, the result Contradictory, so a is telling the truth. If what b said is false, then the murderer is not c, and a is telling the truth, so the murderer is not a, c is telling the truth, so the murderer is d, and d is also telling the truth, so there is a contradiction again. So what b said is also the truth, if c said the lie, then the murderer is not d, what d said is the truth, which is also consistent with the real situation, what b said is also the truth, then the murderer is c, a said The words are also true, so it can be inferred that the murderer is c. So far the inference is over, so if the personal reasoning ability is not enough, how can we let the computer help us infer?

        Let's convert the idea just now into c language code and write it like this. Let's look at the overall code first:

        Here, the blogger has made detailed comments for everyone, so that readers can better understand our code, so I will continue to describe the logical thinking of this code. However, the code is gradually refined from "junk code" to "good code". The process of continuous improvement of program ideas and continuous polishing of code is the process of our programmers' progress. So how can we make this problem of finding the murderer easier?

        First of all, we first define four murderers, a, b, c, d. The target is no longer like the above, but is defined and directly put into the logical judgment, so we use the char type to store. Then define a killer to store our murderer, as follows:

        Next, we need to analyze according to the initial logic. We analyze the logic by constantly adjusting the lying object, so we need to put it in the for loop to repeat the logic. Here, we can directly assign the a member to the killer first, because Among the ASCII code values, the values ​​between a, b, c, and d are all only 1 apart. We only need to increment by 1 in the continuous recycling, which is very convenient, so this is why bloggers directly use their names to cycle s reason,

        So how to judge who is the murderer? Because in c, 0 is false, non-zero is true, and the default is 1. So we add up the words of the four people and put them into the if statement, because there are three true words and one false word, so the added value is 1+1+1+0=3. Note that the last word in this if statement is ==, not =, because there must be three true and one false, we must make it a value of 3 to be logical. Therefore, this simple four or five lines of code can solve this problem, is it much simpler than the previous code!!

 

 

 

 

        

Guess you like

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