【填空】
1、既约分数
问题描述
import os
import sys
import math
# 请在此输入您的代码
count = 0
for i in range(1,2021):
for j in range(1,2021):
if math.gcd(i , j) == 1:
count += 1
print(count)
2、七段码
问题描述
个人思路
- 创建一个存放相邻字幕的字典A
- 全排列所有的元素可能性
- 判断每一个元素是否符合字典A
代码如下
import os
import sys
# 请在此输入您的代码
dic = {
'a':['f', 'b'],
'b':['a', 'c', 'g'],
'c':['b', 'd', 'g'],
'd':['e', 'c'],
'e':['d', 'f', 'g'],
'f':['a', 'e', 'g'],
'g':['b', 'c', 'e', 'f']
}
from itertools import combinations
from itertools import permutations
totle = []
for i in range(2, 8):
a = list(combinations('abcdefg', i)) # 从字符串中生成i个子串的所有组合情况
for j in a:
totle.append(''.join(j)) # ['ab', 'ac', 'ad', ......]
sum = 0 # 统计合法的组合
for s in totle:
for n in permutations(s): # 对totle中的每一个元素都打乱顺序,遍历所有的排序情况,然后分别对照dic查看合不合法
for i in range(len(n)-1):
if n[i+1] not in dic[n[i]]: # 只有所有字符彼此都在自己对应的value中才合法 例如 afd af合法 但fd不合法 所以最终不合法
break
else:
sum += 1
break # 所有排列组合中 只要有一种情况合法 说明其他情况肯定也合法
print(sum + 7) # 最后加上只有一个亮的七种情况