Given: Positive integers n≤100 and m≤20.
Return: The total number of pairs of rabbits that will remain after the n
-th month if all rabbits live for m months.
import sys
def F(n, m):
if n < 2:
return 1
else:
Old = [0, 0, 1]
New = [0, 1, 0]
month = 3
while month <= n:
born_to_die = month - m - 1
if born_to_die < 0:
Died = 0
else:
Died = New[born_to_die]
Old_add = Old[month - 1] + New[month - 1] - Died
New_add = Old[month - 1] - Died
Old.append(Old_add)
New.append(New_add)
month+=1
return Old[n] + New[n] - New[n - m]
def main():
if len(sys.argv) > 1:
print F(int(sys.argv[1]), int(sys.argv[2]))
else:
print 'Enter n and m.'
if __name__ == '__main__':
main()
Return: The total number of pairs of rabbits that will remain after the n
-th month if all rabbits live for m months.
import sys
def F(n, m):
if n < 2:
return 1
else:
Old = [0, 0, 1]
New = [0, 1, 0]
month = 3
while month <= n:
born_to_die = month - m - 1
if born_to_die < 0:
Died = 0
else:
Died = New[born_to_die]
Old_add = Old[month - 1] + New[month - 1] - Died
New_add = Old[month - 1] - Died
Old.append(Old_add)
New.append(New_add)
month+=1
return Old[n] + New[n] - New[n - m]
def main():
if len(sys.argv) > 1:
print F(int(sys.argv[1]), int(sys.argv[2]))
else:
print 'Enter n and m.'
if __name__ == '__main__':
main()
Комментариев нет:
Отправить комментарий