python给定一串数字,求他们两两之间最大的差值

作者: python 发布时间: 2021-02-23 浏览: 2454 次 编辑

给定一串数字,求他们两两之间最大的差值

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用于定义函数中