给定数字序列,如何计算其中任意两数间的最大差值?
- 内容介绍
- 相关推荐
本文共计334个文字,预计阅读时间需要2分钟。
给定一串数字,求它们两两之间最大的差值。
例如,给定数字串 8, 9, 15, 26, 89, 99,如何求它们两两之间最大的差值呢?
现在我来教你,话不多说,上代码:
pythonn=[8, 9, 15, 26, 89, 99]max_diff=max(n) - min(n)print(max_diff)
给定一串数字,求他们两两之间最大的差值
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用于定义函数中
如果你喜欢的话,就不要吝惜你的一键三连了~
谢谢大家!
本文共计334个文字,预计阅读时间需要2分钟。
给定一串数字,求它们两两之间最大的差值。
例如,给定数字串 8, 9, 15, 26, 89, 99,如何求它们两两之间最大的差值呢?
现在我来教你,话不多说,上代码:
pythonn=[8, 9, 15, 26, 89, 99]max_diff=max(n) - min(n)print(max_diff)
给定一串数字,求他们两两之间最大的差值
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用于定义函数中
如果你喜欢的话,就不要吝惜你的一键三连了~
谢谢大家!

