菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

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 = 1f2 = 1foriinrange(1,22): print'%12ld %12ld' % (f1,f2), if(i % 3) == 0: print''f1 = f1 + f2f2 = f1 + f2

实例(Python 3.0+)

#!/usr/bin/python3f1 = 1f2 = 1foriinrange(1,22): print('%12ld %12ld' % (f1,f2), end="")if(i % 3) == 0: print('')f1 = f1 + f2f2 = 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例

AI 思考中...

10 篇笔记 写笔记

  1. #0

    Chivalry

    825***[email protected]

    46

    参考解法:

    #!/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]

    9年前 (2017年04月25日)
  2. #0

    eric

    382***[email protected]

    8

    参考解法:

    #!/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]

    9年前 (2017年04月25日)
  3. #0

    junetest

    499***[email protected]

    4

    参考解法:

    #!/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]

    9年前 (2017年04月30日)
  4. #0

    上官飞鸿

    jac***[email protected]

    12

    使用斐波那契数列:

    # !/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]

    9年前 (2017年05月03日)
  5. #0

    Almighty

    132***[email protected]

    14

    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]

    9年前 (2017年05月28日)
  6. #0

    EvanCode

    ww2***[email protected]

    5

    参考方法:

    #!/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]

    9年前 (2017年07月03日)
  7. #0

    菜鸟鸟

    sun***[email protected]

    2

    参考方法:

    # -*- 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]

    9年前 (2017年08月10日)
  8. #0

    fantasyuj

    jdy***[email protected]

    3

    参考方法:

    #!/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 += a

    fantasyuj

    jdy***[email protected]

    9年前 (2017年09月14日)
  9. #0

    风风

    295***[email protected]

    5

    兔子问题和斐波那契数列差不多,可以用 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]

    9年前 (2017年09月20日)
  10. #0

    Echo

    csz***[email protected]

    10

    这竟然是斐波那契数列,没看出来。我用模拟兔子出生的方式计算的数量:

    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]

    9年前 (2017年12月05日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /