文章目录
一、小明种苹果(续)
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颗苹果的组数,因为题目对组的定义是将苹果树排成了一个圆,于是我就将第一二个苹果情况插入队尾,这样遍历原来的列表就可以判断所有的组。代码没有进行优化,是凭我的第一感觉写的,所以比较冗杂。