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

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

Python 练习实例61

Python 100例 Python 100例

题目:打印出杨辉三角形(要求打印出10行如下图)。

程序分析:无。

程序源代码:

实例

#!/usr/bin/python# -*- coding: UTF-8 -*-if__name__ == '__main__': a = []foriinrange(10): a.append([])forjinrange(10): a[i].append(0)foriinrange(10): a[i][0] = 1a[i][i] = 1foriinrange(2,10): forjinrange(1,i): a[i][j] = a[i - 1][j-1] + a[i - 1][j]fromsysimportstdoutforiinrange(10): forjinrange(i + 1): stdout.write(str(a[i][j]))stdout.write('')print

以上实例输出结果为:

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1

Python 100例 Python 100例

AI 思考中...

8 篇笔记 写笔记

  1. #0

    1234

    123***63.com

    32

    参考方法:

    #! /usr/bin/env python
    # coding:utf-8
    n =10
    def lst(i,j):
     if i==j or j==1:
     return 1
     else:
     return lst(i-1,j-1) + lst(i-1,j)
    for i in range(1,n+1):
     for j in range(1,i+1):
     print lst(i,j),
     print
    

    1234

    123***63.com

    9年前 (2017年05月12日)
  2. #0

    菜鸟py

    928***[email protected]

    2

    参考方法:

    #usr/bin/env python
    #coding:utf-8
    def triangle(line):
     a = []
     for n in range(line):
     a.append([])
     for m in range(n+1):
     if m == 0:
     a[n].append(1)
     elif m == n:
     a[n].append(1)
     else:
     a[n].append(a[n-1][m-1]+a[n-1][m])
     return a
    for i in triangle(11):
     for j in i:
     print j,
     print
    

    菜鸟py

    928***[email protected]

    9年前 (2017年05月19日)
  3. #0

    minions1128

    she***[email protected]

    1

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    yanghui = [[1, 1]] # 初始化
    for i in range(10-2): #打印10行,计算的行数只有8行
     l_temp = [1] #每行的第一个数为1
     for j in range(len(yanghui[i])-1): #遍历上一行
     l_temp.append(yanghui[i][j]+yanghui[i][j+1])
     else:
     l_temp.append(1) #最后一行也为1
     yanghui.append(l_temp) #加入杨辉list中
    yanghui.insert(0, [1]) # 按要求添加第一行的元素
    for i in yanghui:
     print i # 按要求输出
    

    minions1128

    she***[email protected]

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

    强哥1号拖拉机

    249***[email protected]

    4

    参考方法:

    #coding:utf-8
    a = s = [1]
    for i in range(0,10):
     for j in range(i+1):
     if j == 0 or i == j:
     s.append(1)
     print 1,
     else:
     s.append(a[j]+a[j-1])
     print a[j]+a[j-1],
     print
     a,s = s,[]
    

    强哥1号拖拉机

    249***[email protected]

    9年前 (2017年06月28日)
  5. #0

    guanerye

    lis***[email protected]

    1

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    num = 10
    list1 = []
    for i in range(1,num+1):
     list2 = []
     print
     for j in range(1,i+1):
     if j == 1:
     list2.append(1)
     elif i == j:
     list2.insert(j - 1, 1)
     else:
     list2.insert(j - 1, list1[j - 2] + list1[j - 1])
     for k in range(0,len(list2)):
     print list2[k],
     list1 = list2
    

    guanerye

    lis***[email protected]

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

    colinshi

    col***[email protected]

    1

    参考方法:

    #!/usr/bin/python3
    # -*- coding: UTF-8 -*-
    def sj():
     a = [1]
     while True:
     yield a
     a = [sum(i) for i in zip([0] + a, a + [0])]
     
    if __name__=='__main__':
     n=0
     for x in sj():
     print (x)
     n +=1
     if n == 6:
     break
    #关键就是 zip([0]+a,a+[0])

    当第一行的时候只输出[1]

    第二行,sum(i)会产生2个数[1],[1]对应的sum([0]+[a]),sum([a]+[0])

    第三行会产生3个数[1][2][1],看一下ZIP括弧内发生了什么,[0]+[1] [1],[1] [1]+[0]

    zip会把这些数字重新组合成一个数组([0],[1]) ([1],[1]) ([1]+[0])然后分别求和,结果就是[1][2][1]

    第四行继续看zip括弧内发生了什么[0]+[1][2][1] [1][2][1]+[0] 重新zip组合后变成([0],[1]) ([1],[1]) ([2],[1]) ([1],[0])

    colinshi

    col***[email protected]

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

    CosmosHua

    cos***[email protected]

    2

    Python3 下测试:

    #!/usr/bin/python3
    def Pascal(n):
     ls = [[1]]
     for i in range (1, n):
     ls.append([1])
     for j in range(1, i):
     ls[i].append(ls[i-1][j-1] + ls[i-1][j])
     ls[i].append(1)
     for i in range(0, n): print(ls[i])
     return ls
    a = Pascal(10)

    CosmosHua

    cos***[email protected]

    9年前 (2017年07月13日)
  8. #0
    2

    python3 实测通过:

    # -*- coding:UTF-8 -*-
    l=[1]
    for i in range(10):
     for k in l:
     print(k,end='\t')
     print('\n ')
     l1=l[:]
     l1.insert(0,0)
     l1.append(0)
     l2=[]
     for i in range(len(l1)-1):
     l2.append(l1[i]+l1[i+1])
     l=l2[:]
    9年前 (2017年11月29日)

点我分享笔记

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

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