1103. 分糖果 II(Python3)

代码: 

def distributeCandies(candies: int, people: int) -> list:
    # d = [0 for _ in range(people)]
    d = [0] * people
    temp = 1
    while candies > 0:
        for i in range(people):
            if temp > candies:
                d[i] += candies
                candies = -1
                break
            else:
                d[i] += temp
                candies -= temp
                temp += 1
    print(d)
    return d


num_candies = 7
num_people = 4
distributeCandies(num_candies, num_people)

学习内容:

迭代构建数组:

1、构建指定长度全零数组

zero_one = [0] * 6
zero_two = [0 for _ in range(6)]

# zero_one = [0, 0, 0, 0, 0, 0] 
# zero_two = [0, 0, 0, 0, 0, 0]

2、for 循环迭代数组

a = [i * i for i in range(5)]
b = [i - 1 for i in a]

# a = [0, 1, 4, 9, 16]
# b = [-1, 0, 3, 8, 15]

 

发布了29 篇原创文章 · 获赞 2 · 访问量 1394

猜你喜欢

转载自blog.csdn.net/Lucky_Z1111/article/details/104667570