[蓝桥杯] 等差数列 (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))
上一篇:力扣(LeetCode)长按键入 个人题解


下一篇:爱你一生一世