当前位置:   article > 正文

[蓝桥杯] 等差数列 (Python 实现)_python已知等差数列的和,起始,求等差数列

python已知等差数列的和,起始,求等差数列

题目:
在这里插入图片描述
在这里插入图片描述
代码:

def gcd (a, b):
    return gcd(b, a % b) if b else a
n = int(input())
nums = list(map(int,input().split(' ')))
nums.sort()
diff = [nums[i] - nums[i-1] for i in range(1,n)]
temp = gcd(diff[0],diff[1])
for x in range(2,len(diff)):
    temp = gcd(temp, diff[x])
try:
    print(((nums[-1] - nums[0])//temp) + 1)
except:
    print(len(nums))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/774251
推荐阅读
相关标签
  

闽ICP备14008679号