在列中连续打印数字

时间:2021-02-14 09:24:29

标签: python python-3.x

我在 Python 中的模式匹配问题之一中苦苦挣扎

当 input = 3 时,下面是预期的输出(输入值是它应该打印的列数)

预期输出:

1
2 6
3 7 9
4 8
5

不知何故,我正朝着错误的方向前进,因此需要一些帮助。

这是我目前尝试过的代码:

def display(): 
        n = 5
        i = 1
        # Outer loop for how many lines we want to print 
        while(i<=n):  
            k = i 
            j = 1
  
            # Inner loop for printing natural number 
            while(j <= i):  
                print (k,end=" ") 
                  
                # Logic to print natural value column-wise 
                k = k + n - j 
                j = j + 1
                  
            print("\r") 
            i = i + 1
  
#Driver code 
display() 

但它给了我这样的输出:

1 
2 6 
3 7 10 
4 8 11 13 
5 9 12 14 15 

谁能帮我解决这个问题?

4 个答案:

答案 0 :(得分:6)

n=10
for i in range(1,2*n):
    k=i
    for j in range(2*n-i if i>n else i):
        print(k,end=' ')
        k = k + 2*n - 2*j - 2
    print()

结果

1 
2 20 
3 21 37 
4 22 38 52 
5 23 39 53 65 
6 24 40 54 66 76 
7 25 41 55 67 77 85 
8 26 42 56 68 78 86 92 
9 27 43 57 69 79 87 93 97 
10 28 44 58 70 80 88 94 98 100 
11 29 45 59 71 81 89 95 99 
12 30 46 60 72 82 90 96 
13 31 47 61 73 83 91 
14 32 48 62 74 84 
15 33 49 63 75 
16 34 50 64 
17 35 51 
18 36 
19 
> 

答案 1 :(得分:3)

这是一种方法,我从头开始,而不是代码,对我来说更容易

def build(nb_cols):
    values = list(range(1, nb_cols ** 2 + 1))
    res = []
    for idx in range(nb_cols):
        row_values, values = values[-(idx * 2 + 1):], values[:-(idx * 2 + 1)]
        res.append([' '] * (nb_cols - idx - 1) + row_values + [' '] * (nb_cols - idx - 1))

    for r in zip(*reversed(res)):
        print(" ".join(map(str, r)))

答案 2 :(得分:1)

这是一个递归解决方案:

def col_counter(start, end):
    yield start
    if start < end:
        yield from col_counter(start+1, end)
        yield start

def row_generator(start, col, N, i=1):
    if i < col:
        start = start + 2*(N - i)     
        yield start
        yield from row_generator(start, col, N, i+1)

def display(N):
    for i, col_num in enumerate(col_counter(1, N), 1):
        print(i, *row_generator(i, col_num, N))

输出:

>>> display(3)
1
2 6
3 7 9
4 8
5

>>> display(4)
1
2 8
3 9 13
4 10 14 16
5 11 15
6 12
7

>>> display(10)
1
2 20
3 21 37
4 22 38 52
5 23 39 53 65
6 24 40 54 66 76
7 25 41 55 67 77 85
8 26 42 56 68 78 86 92
9 27 43 57 69 79 87 93 97
10 28 44 58 70 80 88 94 98 100
11 29 45 59 71 81 89 95 99
12 30 46 60 72 82 90 96
13 31 47 61 73 83 91
14 32 48 62 74 84
15 33 49 63 75
16 34 50 64
17 35 51
18 36
19

答案 3 :(得分:1)

这是使用简单循环的解决方案

def display(n):
    nrow = 2*n -1  #Number of rows
    i = 1
    noofcols = 1   #Number of columns in each row
    t = 1
    while (i <= nrow):
        print(i,end=' ') 
        
        if i <= n: 
            noofcols = i
        else:
            noofcols = 2*n - i
        m =i
        if t < noofcols:
            for x in range(1,noofcols):
                m = nrow + m -(2*x-1)
                print(m, end=' ')       
            
        i = i+1
        print()