How can python solve Xiaoming's apple planting more concisely
- 2021-10-24 23:20:14
- OfStack
This problem requires us to solve three small problems:
Output the number of apples remaining on all apple trees after all operations; The number of apples falling; The number of groups of apple drops in three adjacent apple trees
The most interesting thing is the third question. According to my understanding, these apple trees are in one column, so we only need to store the status of whether each apple tree drops apples in the list, and then count the number of times that three ones appear continuously in the list. But the N apple trees in the title are arranged in a circle, and the adjacent trees here need to consider the head and tail of the list.
To sum up 1, there are roughly three kinds of practices:
I feel that the above way is too complicated, so I share my code:
for i in range(len(l)):
if l[i-2] and l[i-1] and l[i]:
e += 1
Change the index under 1 and read it directly from the last two elements of the list. If it is all 1, e will add 1 itself. This method seems to be simpler.
At the same time, I also share the codes of the above three situations. You can get what you need:
①
if N >= 3: # There are fewer apple trees 3 Tree time Directly for 0
for y in range(len(is_fall)):
if is_fall[y%len(is_fall)] == is_fall[(y+1)%len(is_fall)] == is_fall[(y+2)%len(is_fall)] == 1:
E += 1
else:
E = 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)]))
③
for i in range(len(is_fall)):
pre=i-1
if pre<0:
pre+=len(is_fall)
next=i+1
if next > len(is_fall)-1:
next-=len(is_fall)
if is_fall[pre]==is_fall[i]==is_fall[next]==1:
E+=1
Here is my solution to this problem:
n = int(input())
result = 0 # The total surplus of apples
count = 0
e = 0 # Continuous 3 The fall of a tree
dl = 0 # Falling apple tree
l = [0] * n # n Is there any apple falling from the apple tree
for i in range(n):
fs = []
zs = []
s = total = 0
x = list(map(int,input().split()))
x = x[::-1]
caozuo = x.pop()
# Determine the number of apples left
for j in range(caozuo):
s = x[j]
if s <= 0:
fs.append(s)
#print(fs)
else:
zs.append(s)
break
result += sum(fs) + zs[0]
total = x.pop()
ss = 0
for j in range(len(x)):
flag = z = 0
s = x.pop()
if s < 0:
ss = ss + s
elif s == 0:
continue
else:
z = s
total = total + ss
if total > z:
flag = 1
else:
flag = 0
l[i] = flag
if flag == 1:
dl += 1
break
else:
continue
for i in range(len(l)):
if l[i-2] and l[i-1] and l[i]:
e += 1
print(result,dl,e)
The idea of the first question is to reverse the list, traverse the list, add complex numbers, stop when positive numbers are encountered, and use the positive numbers plus the previous complex numbers to get the number of apples on all trees in a loop.
The idea of the second question is to use the stack to sum the elements in the pop-up list, stop when encountering positive numbers and compare their sizes to judge whether drops occur.
Above is how python more concise solution of small Ming Apple details, more about python solution of small Ming Apple information please pay attention to other related articles on this site!