如何在Python中对特定范围的彩票游戏进行编码?[已关闭]


有一个抽奖游戏,结果是由数字(1-80)抽出的,每次抽奖由6个数字组成,不重复,那么每个点的组合数是多少?

最低的是1,2,3,4,5,6=21点.最高的是80,79,78,77,76,75=465点.从21-465点,有多少种不同的DIFFERENT组合?(请用Python编码解决)

import random

combo=[]
loop = 0
combination = 0
total = 0

while loop < 6:
    number = random.randint(1,81)
    if number not in combo:
        combo.append(number)
        total += int(number)
        loop +=1
    else:
        continue
    if ((total >= 21 and total <= 465) and (len(combo) == 6)):
        combination +=1
        loop = 0
        combo = []
    total = 0

解决方案:

使用 itertools.combinations

编码

from itertools import combinations

def sum_combs_count(numbers, length):
  " finds count of sum of combinations "
  results = {}

  # Loop over all combinations of numbers of desired length
  for c in combinations(numbers, length):
    k = sum(c)              # sum of numbers
    if not k in results:    # keep track of count in dictionary
      results[k] = 0        # init dictionary key
    results[k] += 1

  return results

# Setup
numbers = list(range(1, 81))  # creates numbers [1, 2, 3, ..., 80]

# Apply function to for combination of numbers of length 6
# for combinations of 80 items taking 6 at a time
# there are 300,500,200 or ~300 million combinations
cnts = sum_combs_count(numbers, 6)

# Display as sum, count pairs from the dictionary
print(cnts )

产量

{21: 1,
 22: 1,
 23: 2,
 24: 3,
 25: 5,
 26: 7,
 27: 11,
 28: 14,
 29: 20,
 30: 26,
 31: 35,
 32: 44,
 33: 58,
 34: 71,
 35: 90,
 36: 110,
 37: 136,
 38: 163,
 39: 199,
 40: 235,
 41: 282,
 42: 331,
 43: 391,
 44: 454,
 45: 532,
 46: 612,
 47: 709,
 48: 811,
 49: 931,
 50: 1057,
 51: 1206,
 52: 1360,
 53: 1540,
 54: 1729,
 55: 1945,
 56: 2172,
 57: 2432,
 58: 2702,
 59: 3009,
 60: 3331,
 61: 3692,
 62: 4070,
 63: 4494,
 64: 4935,
 65: 5427,
 66: 5942,
 67: 6510,
 68: 7104,
 69: 7760,
 70: 8442,
 71: 9192,
 72: 9975,
 73: 10829,
 74: 11720,
 75: 12692,
 76: 13702,
 77: 14800,
 78: 15944,
 79: 17180,
 80: 18467,
 81: 19858,
 82: 21301,
 83: 22856,
 84: 24473,
 85: 26207,
 86: 28009,
 87: 29941,
 88: 31943,
 89: 34085,
 90: 36308,
 91: 38677,
 92: 41134,
 93: 43752,
 94: 46461,
 95: 49342,
 96: 52326,
 97: 55489,
 98: 58763,
 99: 62232,
 100: 65815,
 101: 69605,
 102: 73522,
 103: 77653,
 104: 81919,
 105: 86416,
 106: 91051,
 107: 95929,
 108: 100958,
 109: 106237,
 110: 111675,
 111: 117380,
 112: 123246,
 113: 129391,
 114: 135710,
 115: 142314,
 116: 149099,
 117: 156186,
 118: 163455,
 119: 171037,
 120: 178813,
 121: 186907,
 122: 195201,
 123: 203829,
 124: 212656,
 125: 221827,
 126: 231208,
 127: 240936,
 128: 250878,
 129: 261182,
 130: 271697,
 131: 282582,
 132: 293687,
 133: 305163,
 134: 316861,
 135: 328943,
 136: 341241,
 137: 353929,
 138: 366840,
 139: 380139,
 140: 393660,
 141: 407580,
 142: 421713,
 143: 436248,
 144: 451000,
 145: 466149,
 146: 481511,
 147: 497278,
 148: 513245,
 149: 529617,
 150: 546190,
 151: 563159,
 152: 580321,
 153: 597884,
 154: 615623,
 155: 633759,
 156: 652068,
 157: 670761,
 158: 689615,
 159: 708854,
 160: 728232,
 161: 747987,
 162: 767874,
 163: 788120,
 164: 808481,
 165: 829198,
 166: 850003,
 167: 871151,
 168: 892375,
 169: 913919,
 170: 935517,
 171: 957427,
 172: 979359,
 173: 1001585,
 174: 1023817,
 175: 1046315,
 176: 1068793,
 177: 1091525,
 178: 1114201,
 179: 1137109,
 180: 1159942,
 181: 1182975,
 182: 1205904,
 183: 1229018,
 184: 1251989,
 185: 1275120,
 186: 1298087,
 187: 1321179,
 188: 1344076,
 189: 1367081,
 190: 1389850,
 191: 1412700,
 192: 1435292,
 193: 1457928,
 194: 1480274,
 195: 1502646,
 196: 1524686,
 197: 1546724,
 198: 1568408,
 199: 1590052,
 200: 1611310,
 201: 1632510,
 202: 1653282,
 203: 1673968,
 204: 1694205,
 205: 1714318,
 206: 1733951,
 207: 1753443,
 208: 1772414,
 209: 1791217,
 210: 1809480,
 211: 1827538,
 212: 1845027,
 213: 1862296,
 214: 1878957,
 215: 1895373,
 216: 1911165,
 217: 1926677,
 218: 1941539,
 219: 1956109,
 220: 1969993,
 221: 1983563,
 222: 1996435,
 223: 2008961,
 224: 2020767,
 225: 2032219,
 226: 2042919,
 227: 2053247,
 228: 2062816,
 229: 2071985,
 230: 2080378,
 231: 2088368,
 232: 2095555,
 233: 2102326,
 234: 2108293,
 235: 2113821,
 236: 2118534,
 237: 2122811,
 238: 2126252,
 239: 2129250,
 240: 2131418,
 241: 2133126,
 242: 2134000,
 243: 2134424,
 244: 2134000,
 245: 2133126,
 246: 2131418,
 247: 2129250,
 248: 2126252,
 249: 2122811,
 250: 2118534,
 251: 2113821,
 252: 2108293,
 253: 2102326,
 254: 2095555,
 255: 2088368,
 256: 2080378,
 257: 2071985,
 258: 2062816,
 259: 2053247,
 260: 2042919,
 261: 2032219,
 262: 2020767,
 263: 2008961,
 264: 1996435,
 265: 1983563,
 266: 1969993,
 267: 1956109,
 268: 1941539,
 269: 1926677,
 270: 1911165,
 271: 1895373,
 272: 1878957,
 273: 1862296,
 274: 1845027,
 275: 1827538,
 276: 1809480,
 277: 1791217,
 278: 1772414,
 279: 1753443,
 280: 1733951,
 281: 1714318,
 282: 1694205,
 283: 1673968,
 284: 1653282,
 285: 1632510,
 286: 1611310,
 287: 1590052,
 288: 1568408,
 289: 1546724,
 290: 1524686,
 291: 1502646,
 292: 1480274,
 293: 1457928,
 294: 1435292,
 295: 1412700,
 296: 1389850,
 297: 1367081,
 298: 1344076,
 299: 1321179,
 300: 1298087,
 301: 1275120,
 302: 1251989,
 303: 1229018,
 304: 1205904,
 305: 1182975,
 306: 1159942,
 307: 1137109,
 308: 1114201,
 309: 1091525,
 310: 1068793,
 311: 1046315,
 312: 1023817,
 313: 1001585,
 314: 979359,
 315: 957427,
 316: 935517,
 317: 913919,
 318: 892375,
 319: 871151,
 320: 850003,
 321: 829198,
 322: 808481,
 323: 788120,
 324: 767874,
 325: 747987,
 326: 728232,
 327: 708854,
 328: 689615,
 329: 670761,
 330: 652068,
 331: 633759,
 332: 615623,
 333: 597884,
 334: 580321,
 335: 563159,
 336: 546190,
 337: 529617,
 338: 513245,
 339: 497278,
 340: 481511,
 341: 466149,
 342: 451000,
 343: 436248,
 344: 421713,
 345: 407580,
 346: 393660,
 347: 380139,
 348: 366840,
 349: 353929,
 350: 341241,
 351: 328943,
 352: 316861,
 353: 305163,
 354: 293687,
 355: 282582,
 356: 271697,
 357: 261182,
 358: 250878,
 359: 240936,
 360: 231208,
 361: 221827,
 362: 212656,
 363: 203829,
 364: 195201,
 365: 186907,
 366: 178813,
 367: 171037,
 368: 163455,
 369: 156186,
 370: 149099,
 371: 142314,
 372: 135710,
 373: 129391,
 374: 123246,
 375: 117380,
 376: 111675,
 377: 106237,
 378: 100958,
 379: 95929,
 380: 91051,
 381: 86416,
 382: 81919,
 383: 77653,
 384: 73522,
 385: 69605,
 386: 65815,
 387: 62232,
 388: 58763,
 389: 55489,
 390: 52326,
 391: 49342,
 392: 46461,
 393: 43752,
 394: 41134,
 395: 38677,
 396: 36308,
 397: 34085,
 398: 31943,
 399: 29941,
 400: 28009,
 401: 26207,
 402: 24473,
 403: 22856,
 404: 21301,
 405: 19858,
 406: 18467,
 407: 17180,
 408: 15944,
 409: 14800,
 410: 13702,
 411: 12692,
 412: 11720,
 413: 10829,
 414: 9975,
 415: 9192,
 416: 8442,
 417: 7760,
 418: 7104,
 419: 6510,
 420: 5942,
 421: 5427,
 422: 4935,
 423: 4494,
 424: 4070,
 425: 3692,
 426: 3331,
 427: 3009,
 428: 2702,
 429: 2432,
 430: 2172,
 431: 1945,
 432: 1729,
 433: 1540,
 434: 1360,
 435: 1206,
 436: 1057,
 437: 931,
 438: 811,
 439: 709,
 440: 612,
 441: 532,
 442: 454,
 443: 391,
 444: 331,
 445: 282,
 446: 235,
 447: 199,
 448: 163,
 449: 136,
 450: 110,
 451: 90,
 452: 71,
 453: 58,
 454: 44,
 455: 35,
 456: 26,
 457: 20,
 458: 14,
 459: 11,
 460: 7,
 461: 5,
 462: 3,
 463: 2,
 464: 1,
 465: 1}

给TA打赏
共{{data.count}}人
人已打赏
未分类

在BigQuery中插入datetime对象时,Google Cloud函数崩溃。

2022-9-13 15:24:39

未分类

外接 "C" static void*函数

2022-9-13 15:24:41

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索