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
题目二
def vm(value_matrix, row, col):
L = []
for i in range(len(value_matrix)):
if i+1 == row: # 首先对选取数字所在行进行判断,分情况分析,寻找选取数字所在行的临近值。
row_line = value_matrix[i].split(',')
if col == 1: # 如果列为1,则临近值为该数字右侧第一位,将其添加进列表
L.append(int(row_line[col-1+1]))
elif col == len(value_matrix): # 如果列为最后一列,则临近值为该数字左侧第一位,将其添加进列表
L.append(int(row_line[col-1-1]))
else: # 如果列为中间的某一列,则临近值为该数字左右侧各一位,将其添加进列表
L.append(int(row_line[col-1+1]))
L.append(int(row_line[col-1-1]))
if i+1 == row-1 or i+1 == row+1: # 对选取数字所在行的上下两行进行判断,分情况分析,
# 寻找选取数字所在行的上下两行的临近值。
row_line1 = value_matrix[i].split(',')
print("1-2",row_line1)
if col == 1: # 如果列为1,则临近值为该数字上或下侧第一位和第二位,将其添加进列表
L.append(int(row_line1[col-1]))
L.append(int(row_line1[col-1+1]))
elif col == len(value_matrix): # 如果列为最后一列,则临近值为该数字上或下侧第一位和第二位,将其添加进列表
L.append(int(row_line1[col-1]))
L.append(int(row_line1[col-1-1]))
else: # 如果列为中间的某一列,则临近值为该数字上或下侧的中间三位,将其添加进列表
L.append(int(row_line1[col-1]))
L.append(int(row_line1[col-1-1]))
L.append(int(row_line1[col-1+1]))
return L, max(L)
value_matrix=["6,2,8,6,7", "5,8,1,4,9", "1,3,5,2,1", "4,1,1,4,1", "3,6,5,2,1"]
print(vm(value_matrix, 5, 3))