我来分享一下我的python解法(都是通过的): 1.骰子问题: python里面带Fraction库,用起来简直爽的起飞。 from fractions import Fraction a,b=map(int,input().split()) # a为骰子数,b为大于等于的数 def dicesSum(n,x): results = [] f = [[0 for j in range(6 * n + 1)] for i in range(n + 1)] for i in range(1, 7): f[1][i] = Fraction(1,6) for i in range(2, n + 1): for j in range(i, 6 * n + 1): for k in range(1, 7): if j > k: f[i][j] += f[i - 1][j - k] f[i][j] =Fraction(f[i][j],6) for i in range(n, 6 * n + 1): results.append((i, f[n][i])) res=0 for i in results: if i[0]>=x: res+=i[1] return Fraction(res) print(dicesSum(a,b)) 2.整数移位 a = input() def canForm(a): b = sorted(str(a)) for i in range(2, 10): if sorted(str(a * i)) == b: return True return False while True: try: if canForm(int(input())): print("Possible") else: print("Impossible") except: break
点赞 评论

相关推荐

点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务