(C语言)练习5——矩阵转置

Problem Description
输入N*N的矩阵,输出它的转置矩阵。


Input
第一行为整数N(1≤N≤100)。
接着是一个N*N的矩阵。


Output
转置矩阵。
Example Input

2
1 2
1 2

Example Output

1 1
2 2
程序如下:

#include<stdio.h>
int main()
{
	int i,j,n,a[100][100];
	scanf("%d",&n);
	for(i=0;i<n;i++)
	 for(j=0;j<n;j++)
	{
		scanf("%d",&a[i][j]);
	}
	
/*	for(i=0;i<n;i++)
	{
		int t;
		t=a[i][j];  a[i][j]=a[j][i];  a[j][i]=t;
	}
*/
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			{
				if(j==0) printf("%d ",a[j][i]);
				else printf("%d ",a[j][i]);
			}
		printf("\n");
	}
	return 0;
}

大佬原文程序里,可以不需要中间那段的,尝试过了答案一样。

---------------------
原文:https://blog.csdn.net/Lycodeboy/article/details/53526807
 

猜你喜欢

转载自blog.csdn.net/accumla/article/details/88652207