sdwylry 2020-02-20
目录
输入用了 int(input()) ,但发现这种方法好像有点不方便
每次输入一个数字都要按回车,按空格不能识别
下次找找看看有没有其他输入方法
Python中没有数组,但列表类似数组
通过叠加[ ],就可以通过列表创建二维数组
Python中创建二维数组的方法(以本案例为例):
运行环境: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; }
运行环境: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()