2021中国农业银行春季校园招聘笔试共计三道编程题题目,详解如下:
def list_to_set(ls): len_ls = ls[-1] - ls[0] ls0 = [ls[0]] for i in range(len_ls): ls1 = ls[0] + i + 1 ls0.append(ls1) set0 = set(ls0) return set0 # print(list_to_set([3,4])) def merge_sets(set_list): if len(set_list) == 0: # short circuit to avoid errors return [] current_set = set_list[0] new_set_list = [current_set, ] for s in set_list[1:]: # iterate from the second element if len(current_set.intersection(s)) > 0: current_set.update(s) else: current_set = set(s) # copy new_set_list.append(current_set) return new_set_list import sys s = int(sys.stdin.readline()) ans = [] for i in range(s): l = input().split() temp = [int(j) for j in l] ans.append(list_to_set(temp)) print(ans) print(merge_sets(ans)) print(s - len(merge_sets(ans))) 3 2 4 3 5 6 7