天天看點

給定一串數字,求他們兩兩之間最大的內插補點

給定一串數字,求他們兩兩之間最大的內插補點

hello,大家好,我是Dream。

假如給你8 9 15 26 89 99這一串數字,你如何求他們兩兩之間最大的內插補點呢,現在我教你

話不多說,上代碼:

n = int(input('請輸入個數:'))
ls = input('請輸入數字:').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)
           

在這裡,用到了定義函數的方法,return用于定義函數中

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

謝謝大家!

給定一串數字,求他們兩兩之間最大的內插補點