C代码和python代码:用二维数组实现矩阵的转置
目录
一、总结
输入用了 int(input()) ,但发现这种方法好像有点不方便
每次输入一个数字都要按回车,按空格不能识别
下次找找看看有没有其他输入方法
Python中没有数组,但列表类似数组
通过叠加[ ],就可以通过列表创建二维数组
Python中创建二维数组的方法(以本案例为例):
- matrixA=[[0 for i in range(4)] for i in range(3)]
- matrixA=[ [ 0, 0, 0, 0], [ 0, 0, 0, 0], [ 0, 0, 0, 0]]
二、C代码
运行环境:VS2017
以下是代码:
/* 用二维数组实现矩阵的转置 */ #include <stdio.h> #define ROW 3 #define COL 4 int main() { int matrixA[ROW][COL],matrixB[COL][ROW]; int i,j; printf("Enter elements of the matrixA,"); printf("%d*%d:\n",ROW,COL); for( i=0; i<ROW; i++ ) for( j=0; j<COL; j++ ) scanf_s("%d", &matrixA[i][j]); for( i=0; i<ROW; i++ ) for( j=0; j<COL; j++ ) matrixB[j][i] = matrixA[i][j]; printf("MatrixB,"); printf("%d*%d:\n",COL,ROW); for (i = 0; i < COL; i++) { for (j = 0; j < ROW; j++) printf("%8d", matrixB[i][j]); printf("\n"); } printf("\n Press Any Key to Quit... \n"); getchar(); getchar(); return 0; }
三、Python代码
运行环境:Pycharm,python3.74
以下是代码:
#用二维数组实现矩阵的转置 global ROW,COL ROW=3 COL=4 def main(): matrixA=[[0 for i in range(COL)] for i in range(ROW)] matrixB=[[0 for i in range(ROW)] for i in range(COL)] print("Enter elements of the matrixA,",ROW,COL) for i in range(0, ROW): for j in range(0, COL): matrixA[i][j]=int(input()) for i in range(0, ROW): for j in range(0, COL): matrixB[j][i] = matrixA[i][j] print("MatrixB,", COL, ROW) for i in range(0, COL): for j in range(0, ROW): print(matrixB[i][j],end=" ") print("") print("Press Any Key to Quit...") main()
相关推荐
bizercsdn 2020-04-22
liugan 2020-02-13
sasac 2020-09-25
huangjie0 2020-09-25
cloudking000 2020-09-11
xiaoxiaokeke 2020-07-28
mingyunxiaohai 2020-07-28
honghao0 2020-07-27
风吹夏天 2020-07-26
夕加加 2020-07-20
CallmeZhe 2020-06-29
zhoujiyu 2020-06-28
清风徐来水波不兴 2020-06-16
Happyunlimited 2020-06-15
wanff0 2020-06-14
cuiguanjun 2020-06-13
啸林 2020-06-12
jiayuqicz 2020-06-09
章鱼之家 2020-06-08