[C language]: A murder case occurred somewhere in Japan. The police determined that the murderer must be one of four suspects through investigation.

【Problem Description】:

A murder case occurred somewhere in Japan, and the police determined that the murderer must be one
of . 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 false.
Now, based on this information, write a program to determine who the murderer is.

[Algorithm idea]: The murderer may be one of A, B, C, and D. Because the ASCII codes corresponding to the letters A, B, C, and D are just connected, we can traverse the four of them in turn according to this. Then, using the judgment condition, three of the four people told the truth to screen out the real murderer and then output.

【Reference Code】:
#include<stdio.h>
#include<windows.h>

intmain()
{
	int killer = 0;
	for (killer = 'A'; killer < 'D'; killer++)
		//Use ASCII code to traverse four people continuously
	{
		if ((killer != 'A') + (killer == 'C') + (killer == 'D') + (killer != 'D') == 3)
			//Judging conditions, only one of the three people speaks the truth
		{
			printf("killer is %c\n", killer);
		}
	}
	system("pause");
	return 0;
}

【operation result】:

Reference source: day8_2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325951083&siteId=291194637
Recommended