Solve logic problems with programming

Some people are telling the truth, some are telling lies. These logic questions can be solved by programming. Here are some of the questions that the subject has done, and they are solved with C language and Python.

1. A murder occurred somewhere, and the police determined 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: Yes C.
C said: Yes D.
D said: C is talking nonsense. It is
known that 3 people are telling the truth and 1 person is telling the truth.
Based on this information, determine who is the murderer.

C:

#include<stdio.h>

int main(void)
{
	char muderer;

	//从A到D遍历一遍,假设其中一个是凶手
	for(muderer = 'A'; muderer <= 'D'; muderer++)
	{
		//if语句里为各自说的话,逻辑值加起来为3
		if((muderer != 'A') + (muderer == 'C') + (muderer == 'D') + (muderer != 'D') == 3)
		{
			printf("凶手是%c", muderer);
		}
	}

	return 0;
}

Python:

for muderer in ['A', 'B', 'C', 'D']:
    if((muderer != 'A') + (muderer == 'C') + (muderer == 'D') + (muderer != 'D') == 3):
        print("凶手是%c" % muderer)

2. Five athletes participated in the 10-meter diving competition. Some people asked them to predict the outcome of the competition.
Player A said: B second, I third;
Player B said: I second, E fourth;
Player C said: I first , D second;
D player said: C last, I was third;
E player said: I fourth, A first;
after the game, each player was half right, please confirm the ranking of the game.

C:

#include<stdio.h>

int main(void)
{
	int A, B, C, D, E;

	//由于下面if语句过长,for语句省略大括号
	for(A = 1; A < 6; A++)
	for(B = 1; B < 6; B++)
	for(C = 1; C < 6; C++)
	for(D = 1; D < 6; D++)
	for(E = 1; E < 6; E++)
	if(A != B && A != C && A != D && A != E && B != C && B != D && B != E && C != D && C != E && D != E)
	if(((A==3)+(B==2))==1 && ((B==2)+(E==4))==1 && ((C==1)+(D==2))==1 && ((C==5)+(D==3))==1 && ((A==1)+(E==4))==1)
	printf("A = %d\nB = %d\nC = %d\nD = %d\nE = %d\n", A, B, C, D, E);

	return 0;
}

Python: (no difference from C ...)

for A in range(1, 6):
    for B in range(1, 6):
        for C in range(1,6):
            for D in range(1, 6):
                for E in range(1, 6):
                    if(A != B and A != C and A != D and A != E and B != C and B != D and B != E and C != D and C != E and D != E):
                        if(((A==3)+(B==2))==1 and ((B==2)+(E==4))==1 and ((C==1)+(D==2))==1 and ((C==5)+(D==3))==1 and ((A==1)+(E==4))==1):
                            print("A = %d" % A)
                            print("B = %d" % B)
                            print("C = %d" % C)
                            print("B = %d" % B)
                            print("E = %d" % E)

3. There are three members of A, B and C. A said that B lied, B said that C lied, and C said that A and B lied, judging which of the three told the truth and who told the false.

C:

#include<stdio.h>

int main(void)
{
	int a, b, c; //用abc表示甲乙丙

	//列举所有情况
	for(a = 0; a <= 1; a++)
	for(b = 0; b <= 1; b++)
	for(c = 0; c <= 1; c++)
	if((a&&!b||!a&&b) && (b&&!c||!b&&c) && (c&&a+b==0||!c&&a+b!=0))
	{
		printf("甲说%s\n", a ? "真话" : "假话");
		printf("乙说%s\n", b ? "真话" : "假话");
		printf("丙说%s\n", c ? "真话" : "假话");
	}

	return 0;
}

Python:

for a in range(0, 2):
    for b in range(0, 2):
        for c in range(0, 3):
            if((a and not b or not a and b) and (b and not c or not b and c) and (c and a+b==0 or not c and a+b!=0)):
                print("甲说真话" if(a) else "甲说假话")
                print("乙说真话" if(b) else "乙说假话")
                print("丙说真话" if(c) else "丙说假话")

4. There are five brothers, each saying a word: The
boss said: One of us is lying.
The second child said: Two of us are lying.
The third man said: Three of us lie.
The fourth man said: Four of us lied.
Lao Wu said: All five of us are lying.
Judge who told the truth? Who told lies?

C:

#include<stdio.h>

int main(void)
{
	int a, b, c, d, e;//表示五兄弟

	for(a = 0; a <= 1; a++)
	for(b = 0; b <= 1; b++)
	for(c = 0; c <= 1; c++)
	for(d = 0; d <= 1; d++)
	for(e = 0; e <= 1; e++)
	//列举所有情况 
	if((a&&a+b+c+d+e==4||!a&&a+b+c+d+e!=4) && (b&&a+b+c+d+e==3||!b&&a+b+c+d+e!=3) && (c&&a+b+c+d+e==2||!c&&a+b+c+d+e!=2) && (d&&a+b+c+d+e==1||!d&a+b+c+d+e!=1) && (e&&a+b+c+d+e==0||!e&&a+b+c+d+e!=0))
	{
		printf("老大说的是%s\n", a ? "真话" : "假话");
		printf("老二说的是%s\n", b ? "真话" : "假话");
		printf("老三说的是%s\n", c ? "真话" : "假话");
		printf("老四说的是%s\n", d ? "真话" : "假话");
		printf("老五说的是%s\n", e ? "真话" : "假话"); 
	}

	return 0;
}

Python is similar, so I won't go into details here.

5. Someone in A, B, C broke the glass. A said it was done by B, B said it was done by D, C said that he did not do it, and Ding also said he did not do it.
It is known that three of the four of them have told lies and judged who broke the glass.

C:

#include<stdio.h>

int main(void)
{
	char suspect;

	for(suspect = 'a'; suspect <= 'd'; suspect++)
	if((suspect == 'b') + (suspect == 'd') + (suspect != 'c') + (suspect !='d') == 1)
	switch (suspect)
	{
	case 'a': printf("甲打碎了玻璃"); break;
	case 'b': printf("乙打碎了玻璃"); break;
	case 'c': printf("丙打碎了玻璃"); break;
	case 'd': printf("丁打碎了玻璃"); break;
	default: break;
	}

	return 0;
}

Python:

#利用Python的字典实现C的switch语句
dic = {'a':"甲打碎了玻璃", 'b':"乙打碎了玻璃", 'c':"丙打碎了玻璃", 'd':"丁打碎了玻璃"}
for suspect in ['a', 'b', 'c', 'd']:
    if((suspect == 'b') + (suspect == 'd') + (suspect != 'c') + (suspect !='d') == 1):
        print(dic.get(suspect))
Published 8 original articles · praised 7 · visits 349

Guess you like

Origin blog.csdn.net/weixin_45824303/article/details/105180682