201909-2 小明种苹果(续)Python

一、小明种苹果(续)

1.源代码

N = int(input())
res_drop = []#记录所有树的掉苹果情况
count_drop = 0 #记录掉的苹果数量
res = 0#记录所有的苹果数量
for i in range(N):
    temp = list(map(int,input().split()))
    abs_now =  temp[1]
    mark = 0 #标记这颗树掉了苹果
    ans = 0 #记录每一颗树的苹果数量
    for i in range(temp[0]):
        i += 1
        if i == 1:
            ans += temp[i]
        if i != 1 and temp[i] > 0:
            if ans == temp[i]:
                continue
            else:
                if mark == -1:
                    ans = temp[i]
                    continue
                else:
                    mark = -1
                    ans = temp[i]
                    res_drop.append(1)
                    count_drop += 1
        if temp[i] < 0:
            ans += temp[i]
    res += ans
    if mark == 0:
        res_drop.append(0)
num = 0
#将第一二颗树的情况插入最后,这样就可以判断所有的情况。
res_drop.append(res_drop[0])
res_drop.append(res_drop[1])
for i in range(N):
    if res_drop[i] == 1 and res_drop[i+1] == 1 and res_drop[i+2] == 1:
        num += 1
print(' '.join([str(res),str(count_drop),str(num)]))

总结

本题难点是判断连续掉3颗苹果的组数,因为题目对组的定义是将苹果树排成了一个圆,于是我就将第一二个苹果情况插入队尾,这样遍历原来的列表就可以判断所有的组。代码没有进行优化,是凭我的第一感觉写的,所以比较冗杂。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值