链接
https://www.lanqiao.cn/problems/595/learning/?page=2&first_category_id=1&sort=students_count
题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
小蓝要用七段码数码管来表示一种特殊的文字。
上图给出了七段码数码管的一个图示,数码管中一共有 77 段可以发光的二 极管,分别标记为 a,b,c,d,e,f,g。
小蓝要选择一部分二极管(至少要有一个)发光来表达字符。在设计字符 的表达时,要求所有发光的二极管是连成一片的。
例如:b 发光,其他二极管不发光可以用来表达一种字符。
例如 c 发光,其他二极管不发光可以用来表达一种字符。这种方案与上 一行的方案可以用来表示不同的字符,尽管看上去比较相似。
例如:a,b,c,d,e 发光,f,g 不发光可以用来表达一种字符。
例如:b,f 发光,其他二极管不发光则不能用来表达一种字符,因为发光 的二极管没有连成一片。
请问,小蓝可以用七段码数码管表达多少种不同的字符?
答案
# https://www.lanqiao.cn/problems/595/learning/?page=2&first_category_id=1&sort=students_count
# 学习 itertools 库的使用
# itertools.combinations(s, i + 1) 组合
# itertools.permutations(str1) 排列
import itertools
dict = {'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']}
s = 'abcdefg'
ans = []
for i in range(len(s)):
for x in itertools.combinations(s, i + 1):
ans.append(''.join(x))
res = 0
for str1 in ans:
if len(str1) == 1:
res += 1
continue
for temp in itertools.permutations(str1):
for i in range(1, len(temp)):
if temp[i] not in dict[temp[i - 1]]:
break
else:
res += 1
break
print(res)