逻辑推理与判断(谁是窃贼)

来源:互联网 发布:2017最新交友软件 编辑:程序博客网 时间:2024/05/17 07:09
/**************************************** *  File Name  : reasoning.c *  Creat Data : 2015.1.26*  Author     : ZY *****************************************/ /*逻辑推理与判断*//*谁是窃贼*//*公安人员审问四名窃贼嫌疑犯。已知,这四人当中仅有一名是窃贼,还知道这四个人中每人要么是诚实的要么是说谎的,在回答公安人员的问题中:甲说:“乙没有偷,是丁偷的”   诚实b == 0&&d == 1    说谎b == 1&&d == 0   b+d == 1乙说:“我没有偷,是丙偷的”   诚实b == 0&&c == 1    说谎b == 1&&c == 0   b+c == 1丙说:“甲没有偷,是乙偷的”   诚实a == 0&&b == 1    说谎a == 1&&b == 0   a+b == 1丁说:“我没有偷”             诚实d == 0            说谎d == 1           a+b+c+d == 1 */   /*方法一*/#include <stdio.h>int main(void){int a,b,c,d;for(a = 0;a < 2;a++){for(b = 0;b < 2;b++){for(c = 0;c < 2;c++){for(d = 0;d < 2;d++){if((b+d == 1)&&(b+c == 1)&&(a+b == 1)&&(a+b+c+d == 1)){printf("A %sis the thief.\n",a?"":"not ");printf("B %sis the thief.\n",b?"":"not ");printf("C %sis the thief.\n",c?"":"not ");printf("D %sis the thief.\n",d?"":"not ");}}}}}return 0;}





/*方法二*/#include <stdio.h>int main(void){int i,j,a[4],n;for(i = 0;i < 4;i++)//假定只有第i个人为窃贼{for(j = 0;j < 4;j++)//将第i个人设置为1表示窃贼,其余为0{if(j == i){a[j] = 1;}else{a[j] = 0;}}if(a[3]+a[1] == 1&&a[1]+a[2] == 1&&a[0]+a[1] == 1)//判断条件是否成立{printf("The thief is");for(j = 0;j < 4;j++){if(a[j]){printf(" %c.",j+'A');}}printf("\n");}}return 0;}


0 0
原创粉丝点击