枚举、暴力(python)

指数型枚举

[ 1 , n ] [1, n] [1,n] 内这 n n n 个整数可以随机选取任意多个,输出所有可能的选择方案。

非递归实现

import sys
from itertools import accumulate, permutations, combinations

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")


def read():
    line = sys.stdin.readline().strip()
    while not line:
        line = sys.stdin.readline().strip()
    return map(int, line.split())


def I():
    return int(input())


n = I()
print()
for i in range(1, 1 << n):
    tmp = i
    # print(str(bin(tmp))[2::].zfill(n))
    for j in range(n):
        if (tmp >> j) & 1 == 1:	# 如果i状态下的第j位为1
            print(j + 1, end=' ')
    print()

递归实现

升序
import sys
from itertools import accumulate, permutations, combinations

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")


def read():
    line = sys.stdin.readline().strip()
    while not line:
        line = sys.stdin.readline().strip()
    return map(int, line.split())


def I():
    return int(input())


n = I()
chosen = []


def calc(x):
    if x == n + 1:
        print(*chosen)
        return
    # 不选取x
    calc(x + 1)

    # 选取x
    chosen.append(x)
    calc(x + 1)
    chosen.pop()


calc(n)
降序
import sys
from itertools import accumulate, permutations, combinations

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")


def read():
    line = sys.stdin.readline().strip()
    while not line:
        line = sys.stdin.readline().strip()
    return map(int, line.split())


def I():
    return int(input())


n = I()
chosen = []


def calc(x):
    if x == 0:
        print(*chosen)
        return
    # 不选取x
    calc(x - 1)

    # 选取x
    chosen.append(x)
    calc(x - 1)
    chosen.pop()


calc(n)

组合型枚举

[ 1 , n ] [1, n] [1,n] 内这 n n n 个整数可以随机选取 m m m 个,输出所有可能的选择方案。

非递归实现

import sys
from itertools import accumulate, permutations, combinations

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")


def read():
    line = sys.stdin.readline().strip()
    while not line:
        line = sys.stdin.readline().strip()
    return map(int, line.split())


def I():
    return int(input())


n, m = read()
for t in combinations(range(1, n + 1), m):
    print(*t)

递归实现

import sys
from itertools import accumulate, permutations, combinations

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")


def read():
    line = sys.stdin.readline().strip()
    while not line:
        line = sys.stdin.readline().strip()
    return map(int, line.split())


def I():
    return int(input())


n, m = read()
chosen = []


def calc(x):
    if len(chosen) > m or len(chosen) + (n - x + 1) < m:
        return
    if x == n + 1:
        print(*chosen)
        return
    # 选取x
    chosen.append(x)
    calc(x + 1)
    chosen.pop()

    # 不选取x
    calc(x + 1)


calc(1)

排列型枚举

1 1 1 ~ n n n n n n 个整数排成一行后随机打乱顺序,输出所有可能的次序。

非递归实现

import sys
from itertools import accumulate, permutations, combinations

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")


def read():
    line = sys.stdin.readline().strip()
    while not line:
        line = sys.stdin.readline().strip()
    return map(int, line.split())


def I():
    return int(input())


n = I()
for t in permutations(range(1, n + 1)):
    print(*t)

递归实现

import sys
from itertools import accumulate, permutations, combinations

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")


def read():
    line = sys.stdin.readline().strip()
    while not line:
        line = sys.stdin.readline().strip()
    return map(int, line.split())


def I():
    return int(input())


n = I()

chosen = [0] * (n + 1)  # 表示第i位数字是否被选取
order = [0] * (n + 1)   # 表示选取的第k位数字


def calc(k):
    # 选取第k个数字
    if k == n + 1:
        print(*order[1::])
        return
    for i in range(1, n + 1):
        if chosen[i] == 0:
            order[k] = i
            chosen[i] = 1
            calc(k + 1)
            order[k] = 0
            chosen[i] = 0


calc(1)

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值