数字游戏
时间限制:2 s
内存限制:256 MB
给定一个序列n 正数 a1, a2, ……, an. 只要有不同的数字,就执行以下操作:选择一个最大的数字并从中减去最小的数字。 将执行多少操作?输入输入的第一行包含数字n(1 <n<1000)。下一行包含个数字 ai (1 < ai < 10^9)。输出打印一个数字,操作的次数。
例子
标准输入
2
1 1
标准输出
0
标准输入
3
9 6 3
标准输出
3
标准输入
6
1000000000 1000000000 1000000000 1000000000 1000000000 1
标准输出
4999999995
n = int(input())
a = list(map(int,input().split()))
while True:
count = 0
a.sort()
maxv = a[-1]
minv = a[0]
if maxv==minv:
break
while n > 1:
if a[n-1] == a[0]:
break
elif n>2:
count += (a[n-1]-a[n-2])//a[0] +1
a[n-1] = -(a[0]*((a[n-1]-a[n-2])//a[0] +1)-a[n-1])
a.sort()
print(count)
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
/tmp/ipykernel_176/3611171700.py in <module>
12 break
13 elif n>2:
---> 14 count += (a[n-1]-a[n-2])//a[0] +1
15 a[n-1] = -(a[0]*((a[n-1]-a[n-2])//a[0] +1)-a[n-1])
16 a.sort()
ZeroDivisionError: integer division or modulo by zero
你上一个问题题目下面的所有测试用例, 包括回答下面的测试用例我都试过了, 可以通过, 代码给你试下, 有用点个采纳
n = int(input())
nums = list(map(int,input().split()))
count = 0
while True:
maxV = max(nums)
minV = min(nums)
if maxV == minV: # 结束条件
break
# 以minV做最小值时, 应该减去的值
for i in range(len(nums)):
temp = nums[i] // minV - 1
if temp > 0:
count += temp
nums[i] -= temp * minV
# 更换最小值
maxIndex = nums.index(max(nums))
if nums[maxIndex] == minV:
break
nums[maxIndex] -= minV
count += 1
print(count)
https://ask.csdn.net/questions/7600014?%
一样的问题,已经凉了