代码:
def count1(list):
x=0
for i in range(0,len(list)):
x=int(x)+int(list[i])
avg=x/len(list)
return int(avg)
def main(list):
listfinal=[]
while len(list)>1:
listnew=[]
for i in range(0,len(list)):
# print(list[i],count1(list))
if (list[i]>count1(list)):
listnew.append(list[i])
elif(list[i]==count1(list)):
listnew.append(list[i])
break
list=listnew
return list
if __name__ == '__main__':
list=[2,3,4,7,9,12,33,19,19,33,33]
print('列表',list,'的最大值是',main(list)[0],'。')
优化了下,增加主动输入的能力。
在主函数处,做了以下修改:
if __name__ == '__main__':
# list=[2,3,4,7,9,12,33,19,19,33,1,12233]
x = input('请输入整数列表(非整数会被取整):')
x = x.strip('[')
x = x.strip(']')
list1 = x.split(',')
list1 = [float(list1[i]) for i in range(len(list1))]
list2 = []
for i in range(0,len(list1)):
list2.append(int(list1[i]))
print('输入的列表',list2,'的最大值是',main(list2)[0],'。')