Python 11
[Python 100] Python 100
3
1,1,2,3,5,8,13,21....
(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
f1 = 1
f2 = 1
for i in range(1,22):
print '%12ld %12ld' % (f1,f2),
if (i % 3) == 0:
print ''
f1 = f1 + f2
f2 = f1 + f2
(Python 3.0+)
#!/usr/bin/python3
f1 = 1
f2 = 1
for i in range(1,22):
print ('%12ld %12ld' % (f1,f2), end=" ")
if (i % 3) == 0:
print ('')
f1 = f1 + f2
f2 = f1 + f2
1 1 2 3 5 8
13 21 34 55 89 144
233 377 610 987 1597 2584
4181 6765 10946 17711 28657 46368
75025 121393 196418 317811 514229 832040
1346269 2178309 3524578 5702887 9227465 14930352
24157817 39088169 63245986 102334155 165580141 267914296
[Python 100] Python 100
Chivalry
825***[email protected]
#!/usr/bin/python # -*- coding: UTF-8 -*-# #n=36 def fib(n): if n==1 or n==2: return 1 else: return fib(n-1)+fib(n-2) print fib(36)Chivalry
825***[email protected]
eric
382***[email protected]
#!/usr/bin/python # -*- coding: UTF-8 -*- # time n 3 def rabbit(time,n): if time<1: return 0 elif time==1: num=1 elif time<n: num=1 else: num=rabbit(time-1,n)+rabbit(time-(n-1),n) return num print rabbit(25,3)eric
382***[email protected]
junetest
499***[email protected]
#!/usr/bin/python # -*- coding: UTF-8 -*- def rabbit(num): f1 = 1 #1 f2 = 1 #1 if num == 1 or num == 2: return 1 else: for i in xrange(num-1): f1,f2 = f2,f1+f2 return f1 # print rabbit(36)junetest
499***[email protected]
jac***[email protected]
# !/usr/bin/python # coding=UTF-8 n = int(raw_input(" ")) # f =(1/(5**0.5))*(((1+(5**0.5))/2)**n - ((1-(5**0.5))/2)**n) print "%d%d" % ( n,f)jac***[email protected]
Almighty
132***[email protected]
Python3
#!/usr/bin/python3 def rabbit(n): count = [1,0,0] # for i in range(1,n): # count[2] = count[2] + count[1] count[1] = count[0] count[0] = count[2] return count[0]+count[1]+count[2] # n = int(input("")) rabbit_sum = rabbit(n) print("%d%d"%(n,rabbit_sum))Almighty
132***[email protected]
EvanCode
ww2***[email protected]
#!/usr/bin/env python # -*- coding: UTF-8 -*- def Rabbit(num): i = 1 a,b = 1,1 while i <= num: yield a i += 1 a,b = b,a+b list = [x for x in Rabbit(20)] print(list)EvanCode
ww2***[email protected]
sun***[email protected]
# -*- coding: utf-8 -*- Rabbits={'rabbits':0} # home=[{'rabbits':1}] # month=int(raw_input(':')) time=1 # time while time <month: for j in home: if j['rabbits']>=2: home.append(Rabbits.copy()) else: j['rabbits']+=1 time+=1 print ' %u'% len(home)sun***[email protected]
fantasyuj
jdy***[email protected]
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 1 b = 1 for i in range(1,21,2): print '%d %d'%(a,b), a += b b += afantasyuj
jdy***[email protected]
295***[email protected]
List :
# -*- coding: UTF-8 -*- def rabbit(n): if n == 1: return [1] if n == 2: return [1,1] rabbits = [1,1] for i in range(2,n): rabbits.append( rabbits [-1] + rabbits [-2]) #List return rabbits print rabbit(18) #295***[email protected]
Echo
csz***[email protected]
all_rabbit = [] class Rabbit(): def __init__(self, birthday): self.birthday = birthday all_rabbit.append(self) def makechild(self, month): if month-self.birthday>=2: Rabbit(month) Rabbit(1) for i in range(1, 22): [j.makechild(i) for j in all_rabbit[:]] print(len(all_rabbit))Echo
csz***[email protected]