Given: Positive integers n≤40 and k≤5.
Return: The total number of rabbit pairs that will be present after n
months, if we begin with 1 pair and in each generation, every pair of reproduction-age rabbits produces a litter of k rabbit pairs (instead of only 1 pair).
import sys
def F(n, k):
Fn_2 = 1
Fn_1 = k+1
if n > 1:
i = 3
while i < n:
Fn = Fn_1 + k*Fn_2
Fn_2 = Fn_1
Fn_1 = Fn
i+=1
return Fn
elif n == 1:
return 1
elif n == 2:
return k+1
def main():
if len(sys.argv) > 1:
print F(int(sys.argv[1]), int(sys.argv[2]))
else:
print 'Enter k and n.'
if __name__ == '__main__':
main()
Return: The total number of rabbit pairs that will be present after n
months, if we begin with 1 pair and in each generation, every pair of reproduction-age rabbits produces a litter of k rabbit pairs (instead of only 1 pair).
import sys
def F(n, k):
Fn_2 = 1
Fn_1 = k+1
if n > 1:
i = 3
while i < n:
Fn = Fn_1 + k*Fn_2
Fn_2 = Fn_1
Fn_1 = Fn
i+=1
return Fn
elif n == 1:
return 1
elif n == 2:
return k+1
def main():
if len(sys.argv) > 1:
print F(int(sys.argv[1]), int(sys.argv[2]))
else:
print 'Enter k and n.'
if __name__ == '__main__':
main()
Комментариев нет:
Отправить комментарий