赞
踩
给定数组,我们必须找出最大值,最小值,第二大,第二小数。
算法Step 1: input list element
Step 2: we take a number and compare it with all other number present in the list.
Step 3: get maximum, minimum, secondlargest, second smallest number.
范例程式码# To find largest, smallest, second largest and second smallest in a List
def maxmin(A):
maxi = A[0]
secondsmax = A[0]
mini = A[0]
secondmini = A[0]
for item in A:
if item > maxi:
maxi = item
elif secondsmax!=maxi and secondsmax
secondsmax = item
elif item
mini = item
elif secondmini != mini and secondmini > item:
secondmini = item
print("Largest element is ::>", maxi)
print("Second Largest element is ::>", secondsmax)
print("Smallest element is ::>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。