Python蓝桥杯等级考试真题–第15级
一、 选择题
答案:B
答案:D
解析:集合的并集运算有两种方式,一种是使用“|”运算符进行操作,另一种是使用union()方法来实现,故答案为D。
答案:A
解析:集合是不可以重复的,所以答案为A。
二、编程题
【参考程序】
A = set(input())
B = set(input())
C= A | B # 输出集合A和集合B的并集
print(len(C))
【参考程序】
def check_fortune_cards(cards):
# 去除万能福'X',统计实际收集到的福卡种类
actual_cards = [card for card in cards if card != 'X']
unique_actual_cards = set(actual_cards)
# 初始化需要收集的福卡集合
required_cards = set('12345')
# 检查是否已经收集齐全
if len(unique_actual_cards) == 5:
return "ok"
else:
# 计算还缺少哪些福卡
missing_cards = sorted(list(required_cards - unique_actual_cards))
return ' '.join(missing_cards)
# 输入示例
cards_input = input().strip()
# 调用函数并输出结果
print(check_fortune_cards(cards_input))
解析:首先定义了一个check_fortune_cards函数,用于判断用户是否能参与奖金分享以及输出缺失的福卡。函数通过去除万能福’X’并统计实际收集到的福卡种类来检查是否集齐了五种不同的福卡。如果集齐,则输出"ok";如果没有集齐,则找出并排序缺失的福卡编号后输出。最后,程序读取用户输入的福卡字符串,并调用该函数处理后输出结果。