#include<stdio.h>
#include<stdlib.h>
int main()
{int a,b,c,d,e;
for(a=0;a<2;a++)
for(b=0;b<2;b++)
for(c=0;c<2;c++)
for(d=0;d<2;d++)
for(e=0;e<2;e++)
if(((a==0&&b+c+d+e!=3)||(a==1&&b+c+d+e==3))&&((b==0&&a+c+d+e!=0)||(b==1&&a+c+d+e==0))&&((c==0&&b+a+d+e!=1)|| (c==1&&b+a+d+e==1))&&((d==0&&b+a+c+e!=4)||(d==1&&b+a+c+e==4)))
{ printf("A is pasted a piece of %s paper on his forehead.\n",a?"white":"black");
printf("B is pasted a piece of %s paper on his forehead.\n",b?"white":"black");
printf("C is pasted a piece of %s paper on his forehead.\n",c?"white":"black");
printf("D is pasted a piece of %s paper on his forehead.\n",d?"white":"black");
printf("E is pasted a piece of %s paper on his forehead.\n",e?"white":"black");
}system ("pause");
return 0;
}
5个for循环由里面向外面开始,就是 a=0,b=0,c=0,d=0,e=0为第一个,a=0,b=0,c=0,d=0,e=1为第二个 ,a=0,b=0,c=0,d=1,e=0为第三个以此类推
输出时判断a,b,c,d,e,此时是1还是0,是1就输出white为0输出black
c语言中0是false,非0是true;a?"white":"black" 这句等于 if(a == true)“white”,else “black”。
前面for是循环5层a-e都是从0-2。