题解 | #矩阵转置#
矩阵转置
https://www.nowcoder.com/practice/351b3d03e410496ab5a407b7ca3fd841
#include <stdio.h> int main() { int n = 0; int m = 0; scanf("%d %d", &n,&m); int arr1[n][m];//变长数组不能初始化 int arr2[m][n]; //输入 int i = 0; for(i=0; i<n; i++) { int j = 0; for(j=0; j<m; j++) { scanf("%d", &arr1[i][j]); } } //转置:遍历arr2 for(i=0; i<m; i++) { int j = 0; for(j=0; j<n; j++) { arr2[i][j] = arr1[j][i]; } } //打印 for(i=0; i<m; i++) { int j = 0; for(j=0; j<n; j++) { printf("%d ", arr2[i][j]); } printf("\n"); } return 0; }