Lanqiao Cup basic exercise Yang Hui triangle (implemented in python)

Title description

Resource limit
Time limit: 1.0s Memory limit: 256.0MB

Problem Description
Yang Hui's triangle is also called Pascal's triangle. Its i+1th row is the coefficient of the expansion of (a+b)i. One of its important properties is that each number in the triangle is equal to the sum of the numbers on its two shoulders.
  
The first 4 rows of Yang Hui's triangle are given below:

   1  
  1 1  
 1 2 1 
1 3 3 1

Given n, output its first n lines.

Input format
Input contains a number n

Output format
Output the first n rows of Yanghui triangle. Each line is output in sequence starting from the first number of this line, separated by a space. Please do not output extra spaces in front.

Sample input
4

Sample output
1
1 1
1 2 1
1 3 3 1

Data scale and convention
1 <= n <= 34


Ideas:

  1. We can create a n*n two-dimensional array nums, and replace all the elements with 0 in the first trial
  2. The first number in each row is 1, and the number below is equal to the sum of the left and right numbers above. If i represents the row and j represents the column, then
    nums[ i ][ j] = nums[ i-1 ][ j-1] + nums[ i-1 ][ j]
  3. First, assign values ​​to the elements in the nums two-dimensional array through a loop, and then output each element through a loop. When the element is 0, it is not output.

Code:

n = int(input())

nums = [[0] * n for i in range(n)]

for i in range(n) :
    for j in range(n) :
        if j == 0 :
            nums[i][j] = 1
        else :
            nums[i][j] = nums[i-1][j-1] + nums[i-1][j]
            
for i in range(n) :
    for j in range(n) :
        if nums[i][j] != 0 :
            print(nums[i][j],end=" ")
    print()

Evaluation results:Evaluation results

Code analysis:

  1. Any object can be saved in the list, including lists
my_list = [10,'hello',True,None,[1,2,3]]
print(my_list)
# [10, 'hello', True, None, [1, 2, 3]]
  1. Use * to repeat the elements in the list a specified number of times
num1 = [0]*5
print(num1)           # [0, 0, 0, 0, 0]

num2 = [1,2,3] * 3    # [1, 2, 3, 1, 2, 3, 1, 2, 3]
print(num2)

num3 = [[0] * 5]      # [[0, 0, 0, 0, 0]]
print(num3)

num4 = [[0,0,0]] * 3  # [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
print(num4)
  1. According to the input n, we can create an n * n "two-dimensional array" (actually, it is an element in a list or a list)
  • The correct way to create is like this
n = int(input())
nums = [[0] * n for i in range(n)]
  • Maybe some friends will try to create it like this
num = [[0,0,0]] * 2
print(num)
# 输出:[[0, 0, 0], [0, 0, 0]]

The result seems to be correct, but the program runs incorrectly. The reason is that there is a problem with this creation method.

num[0][0] = 1
print(num)
# 输出:[[1, 0, 0], [1, 0, 0]]

When changing num[0][0], num[1][0] also changes

The above creation method is equivalent to

temp = [0] * 2
num = [temp,temp]
  1. The objects in the list are stored in the list in the order of insertion, and the elements in the list can be obtained by index. The index is an integer starting from 0, the first position of the list is indexed 0, the second position index is 1, the third position index is 2, and so on. If the index is negative, get the element from back to front, -1 means the first to last, -2 means second to last , and so on
num = [[1,2,3],[4,5,6],[7,8,9]]
print(num[-1][-1])
# 输出:9

Guess you like

Origin blog.csdn.net/weixin_43974265/article/details/104874509