CCF-小明种苹果(续)

def check(temp):
    flag = False    #设置一个标记 如果出现掉落的情况 标记设为True
    drop = 0
    pre = temp[1]    
    for i in range(2,len(temp)):
        if(temp[i]>0):
            currenttotal = temp[i]
            if(pre-drop > currenttotal):
                flag = True
            pre = currenttotal
            drop = 0
        else:
            drop+=abs(temp[i])
    left = pre-drop
    return flag,left

N = int(input())      #一共有几颗树
total = []        #每棵树的剩余苹果数量
Flag = []           #每棵树是否存在掉落
E = 0
for i in range(N):
    temp = list(map(int,input().split()))
    flag,left = check(temp)    #返回当前输入的树的掉落标记以及剩余的苹果数
    Flag.append(flag)
    total.append(left)
#当树不足三棵时,苹果掉落情况的组数为0
if(N<3):
    E = 0
elif(N==3):
    if(Flag[0]==True and Flag[1]==True and Flag[0]==True):
        E=3
else:
    for i in range(len(Flag)):
        if(Flag[i%(len(Flag))]==True and Flag[(i+1)%(len(Flag))]==True and Flag[(i+2)%(len(Flag))]==True):
            E+=1
print(sum(total),Flag.count(True),E)

 

你可能感兴趣的:(程序设计(CCF,LeetCode,牛客网))