當前位置:首頁 > IT技術 > 其他 > 正文

給定一串數(shù)字,求他們兩兩之間最大的差值
2022-04-29 14:03:13


給定一串數(shù)字,求他們兩兩之間最大的差值

hello,大家好,我是Dream。

假如給你8 9 15 26 89 99這一串數(shù)字,你如何求他們兩兩之間最大的差值呢,現(xiàn)在我教你

話不多說,上代碼:

n = int(input('請輸入個數(shù):'))
ls = input('請輸入數(shù)字:').split()
def solution(nums,n):
if n==0 or n==1:
return None
elif n==2:
return int(nums[1])-int(nums[0])
else:
max = int(nums[1])-int(nums[0])
fast=2
low=1
while n>fast:
temp = int(nums[fast])-int(nums[low])
if max < temp:
max = temp
fast += 1
low += 1
else:
fast += 1
low += 1
continue
return max
res=solution(ls,n)
print(res)

在這里,用到了定義函數(shù)的方法,return用于定義函數(shù)中

如果你喜歡的話,就不要吝惜你的一鍵三連了~

謝謝大家!給定一串數(shù)字,求他們兩兩之間最大的差值_python



本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務立即開通 >