C language: guess the murderer

topic:

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 .

                    

 =========================================================================

                       

Ideas:

general idea:

In turn assume that each person is a murderer to judge ,

See if what the 4 people said is 1 false and 3 true , it proves that the hypothetical person is the murderer

           

Define variables :

char killer = 0 ; --killer

            

Because the ASCII code values ​​​​of abcd are connected ,

So there is a+1=b , and so on, assuming that everyone is the murderer in turn , judging the situation

(Use a for loop to assume each person is the murderer in turn )

                 

List 4 sentences and 4 situations ,

Case 1 false 3 true , true is 1 , false is 0 ,

4 cases "addition" == 3 ,

(Using the if conditional judgment statement to realize)

That is, the currently assumed person is the murderer , and print

                


                 

complete in one step:

(1).

Define variables :

char killer = 0 ; --killer

            

(2).

Because the ASCII code values ​​​​of abcd are connected ,

So there is a+1=b , and so on, assuming that everyone is the murderer in turn , judging the situation

(Use a for loop to assume each person is the murderer in turn )

                 

(3).

List 4 sentences and 4 situations ,

Case 1 false 3 true , true is 1 , false is 0 ,

4 cases "addition" == 3 ,

(Using the if conditional judgment statement to realize)

That is, the currently assumed person is the murderer , and print

                     

Implementation code:

#include <stdio.h>
int main()
{
	//定义变量:
	char killer = 0; //凶手

	//依次假定每个人是凶手:
	for (killer = 'a'; killer <= 'd'; killer++)
	//因为 a b c d 的ASCII码值是连着的,所以a+1==b,
	//以此类推,依次假定每个人是凶手,判断情况

	{
		//把4个情况列出来:
		if ((killer != 'a') + (killer == 'c') + (killer == 'd') + (killer != 'd') == 3)
		//把4句话,4个情况列出来,情况1假3真,真为1,假为0,4种情况“相加”==3,符合就是凶手进行打印
		{
			//符合则进行打印
			printf("凶手是:%c\n", killer);
			break;
		}
	}


	return 0;
}

Realize the picture:

(Note: The judgment condition of the for loop is killer <= 'd' )

                    

Final code and implementation effect

Final code:

#include <stdio.h>
int main()
{
	//定义变量:
	char killer = 0; //凶手

	//依次假定每个人是凶手:
	for (killer = 'a'; killer <= 'd'; killer++)
	//因为 a b c d 的ASCII码值是连着的,所以a+1==b,
	//以此类推,依次假定每个人是凶手,判断情况

	{
		//把4个情况列出来:
		if ((killer != 'a') + (killer == 'c') + (killer == 'd') + (killer != 'd') == 3)
		//把4句话,4个情况列出来,情况1假3真,真为1,假为0,4种情况“相加”==3,符合就是凶手进行打印
		{
			//符合则进行打印
			printf("凶手是:%c\n", killer);
			break;
		}
	}


	return 0;
}

Realize the effect:

Guess you like

Origin blog.csdn.net/weixin_63176266/article/details/131506800
Recommended