1. 程式人生 > >Old--Magician 經典的Google筆試題

Old--Magician 經典的Google筆試題

Old Magician問題描述:

A magician does the following magic trick. He puts W white balls and B black balls in his hat and asks someone from the audience, say Bob, to remove pairs of balls in whatever order Bob would desire. After removing a pair of balls, Bob is asked to place a white ball back into the hat if they are the same color. Otherwise he is asked to place a black ball into the hat.
When Bob is left with only one ball in the hat, he asks the magician what color the last ball is. Needless to say, the magician can’t see the order by which Bob does the replacements.

The problem is that the magician, like most magicians, is old and sometimes forgets how to do the trick. Being the kind person you are, you are going to help the magician.

For each pair of numbers (W,B) you are asked to output one of the following:

“WHITE” - if the last ball in the hat will be white for sure.
“BLACK” - if the last ball in the hat will be black for sure.
“UNKNOWN” - if you can’t be sure of the last ball’s color.

Input
The first line of the input file contains the number of cases, N. N test cases follow.
Each case contains W and B on a line separated by a space.

Output
For each input case, you should output:
Case #X: Ywhere X is the number of the test case and Y is either “WHITE”, “BLACK” or “UNKNOWN” as explained above. (quotes for clarity)

Limits:0 < N ≤ 1000, W + B > 0
Small dataset: 0 ≤ W ≤ 109, 0 ≤ B ≤ 109

Large dataset: 0 ≤ W ≤ 1000, 0 ≤ B ≤ 1000

Sample:
Input

2
3 1
3 6

Output

BLACK
WHITE

問題分析:
題目的大概意思是,一個魔術師在一個帽子裡面放一下白球和黑球,然後讓一個人每次取兩個球,如果兩個球顏色一樣,就放回一個白球,如果兩個球顏色不一樣,就放回一個黑色的球,問,最後剩下一個球時,是什麼顏色的?

# -*- coding: utf-8 -*-
# @Time   :2018/6/26
# @Author :ShiChao
# title   :Old Magician

import random
# 0代表白,1代表黑
white = int(input("the number of white:"))
black = int(input("the number of black:"))

while (white+black>=2):
    i = random.randint(0, 1)
    j = random.randint(0, 1)
    if (i == 1 and j == 1) and (black>=2):
        black -= 2
        white +=1
    elif (i == 0 and j == 0) and (white>=2):
        white -= 1
    elif (i == 0 and j ==1) and (white>=1 and black>=1):
        white -= 1
    elif (i == 1 and j ==0) and (white>=1 and black>=1):
        white -= 1
    else:
        pass
if (white == 0 and black == 1):
    print("black")
if (white == 1 and black == 0):
    print("white")