Created
April 4, 2021 08:03
-
-
Save Loliver1224/891abe0c9b0398e27b99aaec1f96ff57 to your computer and use it in GitHub Desktop.
SPIの順列問題用に作ったけど出番なかった
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 順列(数字並べて倍数つくるやつ) | |
card = [0,1,2,3,4,5] | |
visited = [False] * len(card) | |
perm = [] | |
def dfs(arr, pos, n): | |
if pos == n: | |
perm.append(arr) | |
return | |
for i in range(len(card)): | |
if not visited[i]: | |
visited[i] = True | |
dfs(arr + [card[i]], pos + 1, n) | |
visited[i] = False | |
dfs([], 0, 3) # 第3引数: 桁数 | |
cnt = 0 | |
for p in perm: | |
if p[0] == 0: continue | |
p = int("".join(map(str, p))) | |
if p % 6 == 0: # 倍数か判定 | |
cnt += 1 | |
print(cnt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment