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

Basic practice of Blue Bridge Cup Yanghui triangle -python|csdn creation punch in

編輯:Python

Blue Bridge Cup Based on practice Yang hui triangle -python-|CSDN Creative punch in

    • Resource constraints
    • Problem description
    • Input format
    • Output format
    • The sample input
    • Sample output
    • Data scale and agreement
    • Implementation code
    • matters needing attention

Resource constraints

The time limit :1.0s Memory limit :256.0MB

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.

Implementation code

n = int(input())
list=[[1]]
row=[]
for i in range(1,n):
row = []
row.append(1)
for m in range(1,i):
row.append(list[i-1][m-1]+list[i-1][m])
row.append(1)
list.append(row)
for a in range(n):
for b in range(a+1):
k=list[a][b]
print(k,end=' ')
print()

matters needing attention

1. Input data type should be converted to int type
2. Clear the current row before writing each row of data
3. When making the display, we need to add one abscissa to the data length we need , You need to pay attention here range The range of a function is a closed before open interval .


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