Therefore I made a representative figure as follows.
In the 1st minute, only one pigeon can be fed.
In the 2nd minute, besides the original pigeon(came in the 1st minute), another two pigeons come.
In the 3rd minute, another three comes beside to the original three (came in the 1st and 2nd minute)
--
According to the aforementioned description, we know the number of new pigeons is exactly the same as the delta of time (in minute)
After understanding the rules above, it is easy to calculate how many pigeons can be fed
--
Following is the source code:
def add_new_pigeons(minute):
return minute
def calculate_total_pigeons(minute):
total = 0
for m in range(1, minute+1):
total += m
return total
def checkio(food):
minute = 1
while (food > 0):
new_group = add_new_pigeons(minute)
total_pigeons = calculate_total_pigeons(minute)
food -= total_pigeons
if food < 0:
new_pigeons = new_group - abs(food)
if new_pigeons < 0: # none of the new pigenons gets fed
new_pigeons = 0
return calculate_total_pigeons(minute - 1) + new_pigeons
elif food == 0:
return total_pigeons
minute += 1
沒有留言:
張貼留言