谁是作案嫌疑人?
Description
刑侦大队对涉及六个嫌疑人的一桩疑案进行分析:
一、a ,b至少有一人作案;
二、a,e,f三人中至少有两人参与作案;
三、 a ,d不可能是同案犯;
四、b,c或同时作案,或与本案无关;
五 c,d中有且只有一人作案;
六 如果d没有参与作案则e也不可能参与作案。
试编写程序,寻找作案人。
Input
多组测试数据,对于每组测试数据,第 1 行输入 6 个空格分隔的整数,代表a、b、c 、d 、e 、f的编号,编号x范围(1 <= x <= 6),且编号互不相同。
Output
对于每组测试数据,第 1 行至第 6 行分别输出对 a、b、c 、d 、e 、f的判断,详细输出格式请参考样例。
Sample
Input
1 2 3 4 5 6
Output
The suspects numbered 1 are criminals.
The suspects numbered 2 are criminals.
The suspects numbered 3 are criminals.
The suspect numbered 4 is not a criminal.
The suspect numbered 5 is not a criminal.
The suspects numbered 6 are criminals.
如果a参与了作案,根据三可知d没有参与,由于d没有参与根据六可知e没有参与,同时根据五可知c参与,根据四b参与,再根据二可知(由于e没有参与)f必然参与
如果b参与了作案,根据上述方式同样可推导至此情况。
#include<stdio.h>
int main()
{
int a,b,c,d,e,f;
while(scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f)!=EOF)
{
printf("The suspects numbered %d are criminals.\n",a);
printf("The suspects numbered %d are criminals.\n",b);
printf("The suspects numbered %d are criminals.\n",c);
printf("The suspect numbered %d is not a criminal.\n",d);
printf("The suspect numbered %d is not a criminal.\n",e);
printf("The suspects numbered %d are criminals.\n",f);
}
}
而将思路过程写入代码过于繁琐
强烈不建议
然而很抱歉的是,老师竟然要求必须用遍历来做…
来个代码吧
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int ch[10],exp[10];//ch[]为犯罪嫌疑人是否为犯罪人,exp[]为6条线索是否符合条件
//0为假,1为真
int main()
{
int a,b,c,d,e,f;
while(scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f)!=EOF)//输入编号
{
for(int i=1; i<=6; i++)
ch[i]=0;//初始化犯罪嫌疑人并不是犯罪人
for(ch[a]=0; ch[a]<=1; ch[a]++)
for(ch[b]=0; ch[b]<=1; ch[b]++)
for(ch[c]=0; ch[c]<=1; ch[c]++)
for(ch[d]=0; ch[d]<=1; ch[d]++)
for(ch[e]=0; ch[e]<=1; ch[e]++)
for(ch[f]=0; ch[f]<=1; ch[f]++)//遍历每一个犯罪嫌疑人分别为犯罪人和不为犯罪人的情况
{
for(int i=1; i<=6; i++)
exp[i]=0;//初始化6条线索不符合条件,下面分别对6条线索进行遍历并检验是否符合如条件
if(ch[a]==1||ch[b]==1)
exp[1]=1;//a ,b至少有一人作案
if((ch[a]==1&&ch[e]==1&&ch[f]==1)||(ch[a]==1&&ch[e]==1&&ch[f]==0)||(ch[a]==1&&ch[e]==0&&ch[f]==1)||(ch[a]==0&&ch[e]==1&&ch[f]==1))
exp[2]=1;//a,e,f中至少有两人参与作案
if((ch[a]==1&&ch[d]==0)||(ch[a]==0&&ch[d]==1)||(ch[a]==0&&ch[d]==0))
exp[3]=1;// a ,d不可能同时案犯
if((ch[b]==1&&ch[c]==1)||(ch[b]==0&&ch[c]==0))
exp[4]=1;//b,c一起
if((ch[d]==0&&ch[c]==1)||(ch[d]==1&&ch[c]==0))
exp[5]=1;//c,d中只有一人作案
exp[6]=1;
if(ch[d]==0&&ch[e]==1)
exp[6]=0;//如果d没有参与作案则e也没有参与作案
if(exp[1]+exp[2]+exp[3]+exp[4]+exp[5]+exp[6]==6)//如果6条线索同时符合则输出
{
if(ch[a]==1)
printf("The suspects numbered %d are criminals.\n",a);
else
printf("The suspect numbered %d is not a criminal.\n",a);
if(ch[b]==1)
printf("The suspects numbered %d are criminals.\n",b);
else
printf("The suspect numbered %d is not a criminal.\n",b);
if(ch[c]==1)
printf("The suspects numbered %d are criminals.\n",c);
else
printf("The suspect numbered %d is not a criminal.\n",c);
if(ch[d]==1)
printf("The suspects numbered %d are criminals.\n",d);
else
printf("The suspect numbered %d is not a criminal.\n",d);
if(ch[e]==1)
printf("The suspects numbered %d are criminals.\n",e);
else
printf("The suspect numbered %d is not a criminal.\n",e);
if(ch[f]==1)
printf("The suspects numbered %d are criminals.\n",f);
else
printf("The suspect numbered %d is not a criminal.\n",f);
}
}
}
}
还是很详细的