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")
试了几个用例都通过了,但感觉是有bug而且还可以优化。欢迎指正。
还可以参考一下这个大神的文章,感觉思路很不错https://blog.csdn.net/XX_123_1_RJ/article/details/80809998