CCF 201703-1 分蛋糕 Python代码

n , k = input().split()
n = int(n)
k = int(k)
a = list(map(int,input().split()))
count = 0 #人数
total = 0

for i in range(len(a)):
    total = total + a[i]
    # 如果重量不大于k 并且已经是最后一块蛋糕了,还是算分到一个人

    if (total < k and i == n - 1):
        count = count + 1

    if (total >= k):
        count = count + 1
        total = 0
        
    # 不满足重量条件不断返回原来叠加
    if (total < k):
        continue

print(count)

注意 if (total < k and i == n - 1):条件必须写在最前面,否则某些情况会少丢掉一个人

猜你喜欢

转载自blog.csdn.net/sinat_27603329/article/details/84641403