程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python (Blue Bridge Cup) basic-6 Yanghui triangle

編輯:Python

Problem description

Yang Hui triangle is also called Pascal triangle , It's the first i+1 Line is (a+b)i The coefficients of the expansion of .

One of its important properties is : Each number in a triangle is the sum of the numbers on its shoulders .

The first part of Yang Hui's triangle is given below 4 That's ok :

1

1 1

1 2 1

1 3 3 1

give n, Output its front n That's ok .

Input format

The input contains a number n.

Output format

Output the front of Yang Hui triangle n That's ok . Each line starts with the first number of the line and outputs in turn , Use a space in the middle . Please don't output extra spaces in the front .

The sample input

4

Sample output

1
1 1
1 2 1
1 3 3 1

Data scale and agreement

1 <= n <= 34.

Ideas :

One 、 Each number is equal to the sum of the two numbers on the previous line . Use this property to write the whole Yanghui triangle . That is to say n+1 OK, No i The number is equal to the n OK, No i-1 Number and number i Sum of numbers , This is also one of the properties of combinatorial numbers . namely C(n+1,i)=C(n,i)+C(n,i-1).
Two 、 The first n Yes m The number can be expressed as C(n-1,m-1), From n-1 Among the different elements m-1 Number of combinations of elements .

n = int(input())
N = [1]
for m in range(n):
for i in range(len(N)):
if i == len(N) - 1:
print(N[i])
else:
print(N[i],end=' ')
N.append(0)
N = [N[k] + N[k-1] for k in range(i+2)]


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved