Data structure and algorithm surface test questions 80 (22)

Source: Internet
Author: User

There are 4 red cards and 4 Blue cards, the host first take any two, and then respectively in A,B,C Three people on the forehead of any two cards affixed,

A,B,C Three people can see the other two people on the forehead of the cards, after reading let them guess what color on their forehead card,

A said do not know,B said do not know,C said do not know, then a said to know.

Ask how to Reason,a is how to know.

If you use the program, how to achieve it ?

Analysis: If the problem is met, everyone has three cases, red, blue, red and blue.

If the presence of three people, there are red, blue, red and blue and three people do not know, then the head of the red and blue people can guess their own color.

If there are three people present (red, blue and blue), then someone must know what color they are.

If there are three people present (red, red, blue), then someone must know what color they are.

If there are three people present (red, red, blue, red, blue), then three of them will not know what color they are.

If there are three people present (red, red, blue, red and blue), then three of them must not know what color they are.

If there are three people present (blue, red, blue, red and blue), then three of them must not know what color they are.

To sum up: a If you know what color, and three people do not know what color they are, the BC must be blue or red or red and red blue, a must be red and blue

Test Instructions: The design of a red and blue circuit when B is red, C is blue, or B is blue, and C is red red. #include <cstdiostruct person{

intX//represents the number of red cards intY//represents the number of blue cards};//Judging if three people don't know and judging if everyone has two cardsBOOLInit (person P1,person P2,person p3) {if(p1.x+p2.x>3|| P1.x+p3.x>3|| P2.x+p3.x>3) {cout<<"someone knows."<<Endl; return false; } if(p1.y+p2.y>3|| P1.y+p3.y>3|| P2.y+p3.y>3) {cout<<"someone knows."<<Endl; return false; } if(p1.x+p1.y!=2|| p2.x+p2.y!=2|| p3.x+p3.y!=2) {cout<<"There's not a number of cards."<<Endl; return false; } if(p1.x<0|| p1.y<0|| p2.x<0|| p2.y<0|| p3.x<0|| p3.y<0) {cout<<" Irrational"<<Endl; return false; } if(p1.x>2|| P1.y>2|| P2.x>2|| P2.y>2|| P3.x>2|| P3.y>2) {cout<<"Non-rule"<<Endl; return false; } return true;}//If the meeting starts three people can not determine their own color A can know, output true, otherwise output falseBOOLYouknow (person P1,person P2,person p3) {if(!init ()) {cout<<"Unreasonable Design"<<endl;return false;} if(p2.x==2&&p3.x==0)return true; Elseifp2.x==0&&p3.x==2)return true; Else return false;}

Data structure and algorithm surface test questions 80 (22)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.