C语言实现逻辑推理

Ddcc 2018年8月31日 09:18 1021916684@qq.com
C语言 逻辑推理 A.本题没有准确答案

如需转载请注明出处:http://zczzxz.top,整理不易请谅解。

以下选项唯一正确的是(),请用C语言实践逻辑推理。

A.本题没有准确答案

B.本题有多个准确答案

C.D和E都不正确

D.B和C有一个正确

E.C不正确

F.D和F有一个正确

给出c语言的代码:


#include "stdio.h"
#include "stdbool.h"

#define getbit(x, y) ((x)>>(y)&1)

bool AnswerContentA(char Answer) {
	return !(Answer | 0x00);
}

bool AnswerContentB(char Answer) {
	return (Answer > 0x01);
}

bool AnswerContentC(char Answer) {
	return (getbit(Answer, 1) == 0) && (getbit(Answer, 2) == 0);
}

bool AnswerContentD(char Answer) {
	return ((getbit(Answer, 3) == 1) && (getbit(Answer, 4) == 0)) | ((getbit(Answer, 3) == 0) && (getbit(Answer, 4) == 1));
}

bool AnswerContentE(char Answer) {
	return (getbit(Answer, 3) == 0);
}

bool AnswerContentF(char Answer) {
	return (getbit(Answer, 2) == 1) || (getbit(Answer, 0) == 1);
}

char get_answer(void){
	char answer[6] = {'A','B','C','D','E','F'};
	char count = 0x3F;
	char result = 0;
	while(count--){
		result = (char)AnswerContentA(count) +  (char)AnswerContentB(count) + (char)AnswerContentC(count)+\
				(char)AnswerContentD(count) +  (char)AnswerContentE(count) + (char)AnswerContentF(count);
		if(1 == result){
			break;
		}
	}
	printf("%d\n", count);
	result = AnswerContentA(count) |  AnswerContentB(count)<<1 | AnswerContentC(count)<<2 |\
				AnswerContentD(count)<<3 |  AnswerContentE(count)<<4 | AnswerContentF(count)<<5;
	return answer[result/2];
}

void main(void){
	printf("%c", get_answer());
} 
我们将答案看成一个char Answer类型的数字,转化为2进制(前面两个为填充0)。A选项为都不正确,翻译为c语言为Answer满足!(Answer|0x00)(Answer全为0),以此类推其它答案。最后我们遍历一下答案,即从6个1,遍历到全为0,如果满足所有答案返回值得和为1,则满足题目要求的答案并找到了选项,找到那个返回1的答案即可。