某公司要从赵、钱、孙、李、周五名新毕业的大学生中选派一些人出国学习.选派必满足以下条件:
(1)若赵去,则钱也去.
(2)李、周两人中必有一人去.
(3)钱、孙两人中去且仅去一人.
(4)孙、李两人同去或同不去.
(5)若周去,则赵、钱也同去.
#include <stdio.h>
int main(){
int p=0;//p表示赵去;q表示钱去;r表示孙去;s表示李去;t表示周去
int q=0;
int r=0;
int s=0;
int t=0;
int e[6];
int count=0;
//通过for循环来模拟真值表,最终通过
//if语句判断来得到结果
for(p=0;p<=1;p++){
for(q=0;q<=1;q++){
for(r=0;r<=1;r++){
for(s=0;s<=1;s++){
for(t=0;t<=1;t++){
//将题目中的条件符号化,用代码实现
e[1]=!p||q;
e[2]=(s&&!t)||(!s&&t)||(s&&t);
e[3]=(q&&!r)||(!q&&r);
e[4]=(r&&s)||(!r&&!s);
e[5]=!t||(p&&q);
if(e[1]&&e[2]&&e[3]&&e[4]&&e[5]){
count++;
if(count>1)printf("或");
if(p)printf("赵去");
if(q)printf("钱去");
if(r)printf("孙去");
if(s)printf("李去");
if(t)printf("周去");
printf("\n");
}
}
}
}
}
}
return 0;
}
结果如下:
孙去李去
赵去钱去周去
--------------------------------
Process exited after 0.01864 seconds with return value 0
请按任意键继续. . .