ccf 小明种苹果(续) (Python)

原题

在这里插入图片描述
在这里插入图片描述
官网放图真受不了,案例一个一个敲的

测试样例

"""
5
4 10 0 9 0 
4 10 -2 7 0
2 10 0
4 10 -3 5 0
4 10 -1 8 0

4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0
"""

思路

比如输入5 73 -8 -6 59 -4这段数据,
用temp保存,剔除第一个(python的len函数替代),成为73 -8 -6 59 -4,然后选定第一个73,往后遇到非正数,就加,73-8-6=59,遇到正数59,就替换成59,如果替换前的数值大于替换后,给temp后面加一个标记"False",表示有掉落,D+1,
同时每个temp算出来的sum加到T中
最后再统计False出现的相邻情况即可

源代码

# create by 15574946385 at 2019/10/4
# content:
n=int(input())
tree=[]
T,D,E=0,0,0
for i in range(n):
    temp=list(map(int,input().split()))
    temp=temp[1:]
    tempSum=temp[0]
    if len(temp)>1:
        for i in range(1,len(temp)):
            if temp[i]<=0:
                tempSum+=temp[i]
            elif tempSum>temp[i]:
                if temp[-1]!="False":
                    temp.append("False")
                    D+=1
                tempSum=temp[i]
        T+=tempSum
    tree.append(temp)
for i in range(len(tree)):
    pre=i-1
    if pre<0:
        pre+=len(tree)
    next=i+1
    if next>len(tree)-1:
        next-=len(tree)
    if tree[pre][-1]==tree[i][-1]==tree[next][-1]=="False":
        E+=1
print(T,D,E)

发布了106 篇原创文章 · 获赞 89 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_37465638/article/details/102060984