对于下面给出的以下示例,我需要一个代码。这是我尝试的代码,矩阵末尾不应包含换行符(\ n):
r=int(input())
c=int(input())
count=1
for i in range(r):
for j in range(c):
print(count)
count=count+1
print()
输入
r=2
c=2
输出
1 2
3 4
答案 0 :(得分:0)
from __future__ import print_function
m = int(input("Enter the row:"))
n = int(input("Enter the column:"))
num = 1
# iterate over the rows and print each row
for i in range(m):
row = " ".join(map(str, range(i*n + 1, (i+1)*n + 1)))
print(row)
由于对问题进行了编辑:
m = int(input("Enter the row:"))
n = int(input("Enter the column:"))
count = 1
for i in range(m):
for j in range(n):
print(count, end=" ")
count += 1
print()
答案 1 :(得分:0)
r = int(input())
c = int(input())
count = 1
for i in range(r):
for j in range(c):
print(count,end=' ')
count=count+1
print()
答案 2 :(得分:0)
关闭代码:
def print_matrix(rows, columns):
# maximum width of column, including spaces
pad_len = len(str(rows * columns)) + 1
for y in range(rows):
for x in range(columns):
print("{n:>{pad}}".format(
n=y * columns + x + 1,
pad=pad_len
), end='')
print()
print_matrix(18, 37)
我已将计数器替换为根据x
和y
计算得出的值;希望您能看到如何完成。而且,它使用填充,以便即使是很大的数字,列也始终对齐。