C language of black and white issue

Problem Description:

         There are A, B, C, D, E five people, each person on the forehead of a black or white note paper. 5 people on the ride, everyone can see the color of the paper on someone else forehead. 5 people observed after each other:

  • A: "I saw three people posted on the forehead it is white and one is black paper posted on the forehead."
  • B said: "I saw four others are black paper posted on the forehead."
  • C said: "I saw one person posted on the forehead is white, the other three people posted forehead is black paper."
  • D said: "I saw four people posted on the forehead is white."
  • E did not say anything.


Now known head saying said black paper is lie, her forehead against the white man telling the truth. Ask five people who posted forehead is white and who is black forehead posted paper?

problem analysis:

Uh uh, just launched to the correct answer, asked school brother can not write in code ...

Analysis of each person to say, for everyone, there are only two cases, lying or telling the truth, the enumeration of all violence, the right answer can be elected

The key to solve the problem is the logic class to write the correct logical expressions. The Condition defines a clear description of the analysis are listed in program language can be obtained after the final determination result using the exhaustion method.

0 is represented by black paper paste, a paste is represented by white

Analyzing conditions:

1 (a && (b+c+d+e==3) || !a && (b+c+d+e!=3)) &&
2 (b && (a+c+d+e==0) || !b && (a+c+d+e!=0)) &&
3 (c && (a+b+d+e==1) || !c && (a+b+d+e!=1)) &&
4 (d && (a+b+c+e==4) || !d && (a+b+c+e!=4))

Code:

1 #include <stdio.h>
 2  int main ()
 . 3  {
 . 4      int A, B, C, D, E;   / * 0 representing black and 1 for white * / 
. 5      for (A = 0 ; A <= 1 ; a ++)   / * exhaustive five forehead note paper color of all possible * / 
. 6          for (B = 0 ; B <= . 1 ; B ++ )
 . 7              for (C = 0 ; C <= . 1 ; C ++ )
 . 8                  for (D = 0 ; D <= . 1 ; D ++ )
 . 9                      for (E = 0; e<=1; e++)
10                         if( (a && (b+c+d+e==3) || !a && (b+c+d+e!=3)) &&
11                             (b && (a+c+d+e==0) || !b && (a+c+d+e!=0)) &&
12                             (c && (a+b+d+e==1) || !c && (a+b+d+e!=1)) &&
13                             (d && (a+b+c+e==4) || !d && (a+b+c+e!=4))
14                         )
15                         {
16                             printf(". Sticker A forehead% s color \ n- ' , A? " White " : " black " );
 . 17                              the printf ( " sticker on B forehead% s color \ n-. " , B? " White " : " black " );
 18 is                              the printf ( " sticker C forehead% s color \ n-. " , C? " white " : " black " );
 . 19                              the printf ( " sticker on D forehead% s color . \ the n- " ,d?"": " Black " );
 20 is                              the printf ( " sticker on his forehead is% s E color \ n-. " , E? " White " : " black " );
 21 is                          }
 22 is      return  0 ;
 23 is }
View Code

 

 

Guess you like

Origin www.cnblogs.com/chuyds/p/11986339.html