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]

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值