首页 > 试题广场 >

题目标题: 二维数组转置

[问答题]

题目标题:

二维数组转置

题目描述:

编程实现使给定的一个N×M的二维整型数组转置,即行列互换。

输入描述:

第一行是两个整数NM ,接下来N行整数,每行包含M个整数。整数之间用空格隔开

输出描述:

输出转置后的二维整型数组.每一个数据之间用一个空格隔开,但每行的最后一个数据后不能有空格.所有输出的最后一行不能有回车

样式输入:

2 3

1 2 3

4 5 6

样式输出:

1 4

2 5

3 6

import numpy as np

R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))

print("Enter the entries in a single line (separated by space): ")

# User input of entries in a
# single line separated by space
entries = list(map(int, input().split()))

# For printing the matrix
matrix = np.array(entries).reshape(R, C)
print(list(map(list, zip(*matrix))))

'''
Enter the number of rows:3
Enter the number of columns:4
Enter the entries in a single line (separated by space): 
1 2 3 4 5 6 7 8 9 0 1 2
[[1, 5, 9], [2, 6, 0], [3, 7, 1], [4, 8, 2]]
'''

发表于 2019-12-30 20:00:13 回复(0)

#include<stdio.h>

int main()

{

int m,n,a[20][20];

int i=0,j=0 ;

scanf("%d%d",&m,&n);

for(i=0;i<m;i++)

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

for(i=0;i<n;i++){

for(j=0;j<m;j++){

printf("%d",a[j][i]);

if(j!=m-1) printf(" ");

}

if(i!=n-1) printf("\n");

}

return 0 ;

}

发表于 2017-05-17 04:31:53 回复(0)