矩阵转置 - C语言 C++ Java Python

矩阵转置 - C语言 C++ Java Python

输入矩阵的行和列以及矩阵各个元素,输出它的转置矩阵。
C语言:

#include <stdio.h>

int main()
{
	int line, colums;
	printf("输入矩阵行列\n");
	scanf("%d%d", &line, &colums);
	int array[line][colums];
	for(int i = 0;i < line;i++)
		for(int j = 0;j < colums;j++)
			scanf("%d", &array[i][j]);
	printf("\n原矩阵\n");		
	for(int i = 0;i < line;i++){
		for(int j = 0;j < colums;j++)
			printf("%d ", array[i][j]);
		printf("\n");
	}
	printf("\n转置矩阵\n");		
	for(int j = 0;j < colums;j++){
		for(int i = 0;i < line;i++)
			printf("%d ", array[i][j]);
		printf("\n");
	}	
	
	return 0;
} 
/* Code Running Results
输入矩阵行列
2 3
1 2 3
4 5 6

原矩阵
1 2 3
4 5 6

转置矩阵
1 4
2 5
3 6
*/ 

以下 C++ 的代码与 C语言 几乎是一样的,是一样的道理,都是使用二维数组来实现矩阵的转置,只是输入输出换了一种编写方式。
C++:

#include <iostream>

using namespace std;

int main()
{
	int line, colums;
	cout<<"输入矩阵行列"<<endl;
	cin>>line>>colums;
	int array[line][colums];
	for(int i = 0;i < line;i++)
		for(int j = 0;j < colums;j++)
			cin>>array[i][j];
	cout<<endl<<"原矩阵"<<endl;		
	for(int i = 0;i < line;i++){
		for(int j = 0;j < colums;j++)
			cout<<array[i][j]<<" "; 
		cout<<endl;
	}
	cout<<endl<<"转置矩阵"<<endl;
	for(int j = 0;j < colums;j++){
		for(int i = 0;i < line;i++)
			cout<<array[i][j]<<" "; 
		cout<<endl;
	}	
	
	return 0;
} 
/* Code Running Results
输入矩阵行列
2 3
1 2 3
4 5 6

原矩阵
1 2 3
4 5 6

转置矩阵
1 4
2 5
3 6
*/ 

Java 自然也可以使用二维数组实现矩阵的转置,但是那样大可不必再重复一遍。
可以换一种方式,把二维数组换成存放数组列表的数组列表 ( 即 ArrayList<ArrayList> ) 。
Java:

import java.util.ArrayList;
import java.util.Scanner;

public class TestTwo extends Thread{
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入矩阵行列");
		int line = sc.nextInt();
		int colums = sc.nextInt();
		
		ArrayList<ArrayList<Integer>> arraylistline = new ArrayList<ArrayList<Integer>>();
		for(int i = 0;i < line;i++) {
			ArrayList<Integer> arraylistcolumns = new ArrayList<Integer>();
			for(int j = 0;j < colums;j++)
				arraylistcolumns.add(sc.nextInt());
			arraylistline.add(arraylistcolumns);
		}
		
		System.out.println("原矩阵");
		for(int i = 0;i < line;i++) 
			System.out.println(arraylistline.get(i));
		
		System.out.println("\n转置矩阵");
		ArrayList<Integer> transposematrix = new ArrayList<Integer>();
		for(int j = 0;j < colums;j++) {
			for(int i = 0;i < line;i++) 
				transposematrix.add(arraylistline.get(i).get(j));
			System.out.println(transposematrix);
			transposematrix.clear();
		}
		
		sc.close();
	}
}
/* Code Running Result
请输入矩阵行列
2 3
1 2 3
4 5 6
原矩阵
[1, 2, 3]
[4, 5, 6]

转置矩阵
[1, 4]
[2, 5]
[3, 6]
*/

接下来就要提现 Python 的强大了
Python3:

matrix = []
print("请输入矩阵行列")
line = int(input())
colums = int(input())
for x in range(line):
    row = []
    for y in range(colums):
        temp = int(input())
        row.append(temp)
    matrix.append(row)

print("原矩阵")
row = []
for row in matrix:
    print(row)

print("转置矩阵")
row = []
transposed = [[row[i] for row in matrix] for i in range(colums)]  # 矩阵转置
for row in transposed:
    print(row)

#Code Running Result
# 2
# 3
# 1
# 2
# 3
# 4
# 5
# 6
# [1, 2, 3]
# [4, 5, 6]
# [1, 4]
# [2, 5]
# [3, 6]

transposed = [[row[i] for row in matrix] for i in range(colums)]

仅用了这一行就实现了矩阵的转置

posted @ 2020-10-05 00:23  狡猾的狐狸科  阅读(358)  评论(0编辑  收藏  举报