CSDN Black and white ball (logic question)

Source: Internet
Author: User

Title meaning: Black and white Ball

http://student.csdn.net/mcs/question_detail/597

A bag with two colors of ball, black and white, you take out two balls each time, if the same color, then put a white ball in the bag, or put back a black ball.

Can you finally determine the color of the ball when there is a ball left in the last bag?

Input format:

Multiple sets of test data, one row per set of data, with two numbers. W and B. (0<=w,b<=1000000000, W + B > 0), respectively, indicating the number of white and black balls.

Output format:

If you can make sure that the final white ball is left, the output will be black, otherwise the output unknown if it can be determined that there will be a certain remaining dark ball.

Topic Analysis:

Sub-situation to take the ball on the line, (at the beginning also did not want to directly see the idea of the topic hint)

There are three kinds of ball-taking situations:

1, take a black and white, put back a black ball, the equivalent of taking out a white ball.

2, take two white ball, put back a white ball, the equivalent of taking out a white ball.

3, take two black ball, put back a white ball, equivalent to remove two black ball, put back a white ball.

Through the above analysis we found that the white ball can be taken out one at a time, and black ball can only be taken out, it is clear that we have to analyze the number of black balls.

When the number of black balls is odd, the black ball must not be exhausted, and finally left a black ball, white ball no matter how many, through the first two methods each time out one, can be taken out each, until the end, and finally left a black ball.

When the number of black balls is even =, if the black ball first go to the left, then the white ball, according to the second situation every time a white ball, and finally left a white ball can not be taken out; if the black ball is not exhausted, the white ball according to the first two conditions, the last remaining two black balls, then finally = one time according to the situation three out of two black ball back Finally, there is a white ball left.


AC Code:

#include <iostream>using namespace Std;int main () {    long long w,b;    while (cin>>w>>b) {        if (w==1&&b==0) {            cout<< ' white ' <<endl;            Continue;        }        if (w==0&&b==1) {            cout<< "BLACK" <<endl;            Continue;        }        if (b%2==0) cout<< "white" <<endl;        else cout<< "BLACK" <<endl;    }    return 0;}


CSDN Black and white ball (logic question)

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.