题解 | #用rand5()实现rand7()#

用rand5()实现rand7()

https://www.nowcoder.com/practice/581fb9be10d647e7bb179234b42f3f5f

import random
#coding:utf-8
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
# rand5() is defined
# @return a random integer in the range 1 to 7
#
class Solution:
    def rand5(self):
        return random.randint(1, 5)
    def rand7(self) -> int:
        # write code here
        while 1:
            res = 5*(self.rand5()-1) + self.rand5()-1
            if res >= 21:
                continue
            return 1+res%7

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务