C language guess the murderer and similar problems

describe:

A murder case occurred somewhere in Japan, and the police determined that the murderer must be one of four suspects through investigation.

The following are the confessions of the four suspects:

A said: Not me.        

B said: yes C.

C said: yes D.

D said: C is talking nonsense

3 people are known to tell the truth and 1 person is known to be false.

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

Ideas and Analysis:

        Students who have never done a similar problem may be a little confused when they see this problem for the first time. Then start thinking about assumptions, permutations, enumerations, etc... but the code results for this question will surprise you.

        The first thing to consider is how to represent the words of the four people A, B, C, and D in code? We define a char variable killer to represent the killer. The most conceivable thing is to set the result to 1 (similar to a bool type) if what someone said is true, and 0 otherwise. Such thinking is correct. A says, not A, then, it can be expressed as killer != 'A'. If the judgment is true, it will return 1, otherwise, it will return 0. Similarly, what the other 3 people say can also be expressed in this way. Finally, based on 3 people telling the truth and 1 telling the truth, the sum returned is 3. For example, if we assume that A is the murderer, then according to the words of the four people, A told a lie and B also lied, which does not meet the conditions. After this, only the situation of A has been judged, and the situation of B, C, and D has not yet been judged. It is only necessary to add a loop.

        The C language code is as follows:

#include<stdio.h>

int main(void)
{
	char killer = 'A';
	for (killer = 'A'; killer <= 'D'; killer++)
	{
		if ((killer != 'A') + (killer == 'C') + (killer == 'D') + (killer != 'D') == 3)
		{
			printf("the killer is %c", killer);
		}
	}

	return 0;
}

        The result is as follows:

        

         Now, let's look at a second question similar to it:

describe:

        Two table tennis teams compete. Team A has three players ABC ; Team B has three players XYZ ; draw lots to decide the match list, someone asks the players for the match list, A says he doesn’t compete with X , C says he doesn’t compete with Z . Please program to output all possible matchup plans and count the number of plans.

Ideas and Analysis:

        This question is the same as the method of guessing the murderer, which directly converts the text description of the title into a code description. Just use the for loop directly. code show as below:

#include<stdio.h>

int main(void) 
{
	char A = 0;
	char B = 0;
	char C = 0;
		for (A = 'X'; A <= 'Z'; A++)
		{
			for (B = 'X'; B <= 'Z'; B++)
			{
				for (C = 'X'; C <= 'Z'; C++)
				{
					if ((A != 'X') + (C != 'Z')  == 2)
					{
						if (A != B && B != C && C != A)
						{
							printf("A VS %c, B VS %c, C VS %c\n", A, B, C);
						}
						
					}
				}
			}
		}
	return 0;
}

        The result is as follows:

        Third topic:

describe:

Five athletes participated in the 10-meter platform diving competition and were asked to predict the results 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 contestant is half right, please program to determine the ranking of the game.

Ideas and Analysis:

        Brute-force cracking. code show as below:

#include<stdio.h>

int main(void)
{
	int a = 0, b = 0, c = 0, d = 0, e = 0;

	for (a = 1; a <= 5; a++)
	{
		for (b = 1; b <= 5; b++)
		{
			for (c = 1; c <= 5; c++)
			{
				for (d = 1; d <= 5; d++)
				{
					for (e = 1; e <= 5; e++)
					{
						if (((b == 2) + (a == 3) == 1) && //B第二,我第三
							((b == 2) + (e == 4) == 1) && //我第二,E第四
							((c == 1) + (d == 2) == 1) && //我第一,D第二
							((c == 5) + (d == 3) == 1) && //C最后,我第三
							((e == 4) + (a == 1) == 1))   //我第四,A第一
						{
							if (a * b * c * d * e == 120)
							{
								printf("%d %d %d %d %d\n", a, b, c, d, e);
							}	
						}
					}
				}
			}
		}
	}
	return 0;
}

 

Summarize:

        From the last question, there are astonishing five for loops. At the same time, each for loop loops 5 times, and the time complexity reaches O(n * n). That is to say, once the number of people to be judged increases, the calculation speed of the computer will slow down at a speed visible to the naked eye. For similar problems, the brute force method is not the optimal solution, but it is the most intuitive, simple and easy-to-understand method. 

Guess you like

Origin blog.csdn.net/Naion/article/details/121884821