Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入N*N的矩阵,输出它的转置矩阵。
Input
第一行为整数N(1≤N≤100)。
接着是一个N*N的矩阵。
Output
转置矩阵。
Sample Input
2
1 2
1 2
Sample Output
1 1
2 2
Hint
Source
ZJGSU
**本题的核心语句为b[j][i] = a[i][j];
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int a[100][100],b[100][100]; //定义两个数组:a数组为存放原数据,b数组为转置后的数组;
int i,j;
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++)
{
for(j=0;j<n;j++)
{
b[j][i] = a[i][j]; //转置后存放在b数组;
}
}
for(i=0;i<n;i++) // 输出
{
for(j=0;j<n;j++)
{
if(j==n-1)
printf("%d\n",b[i][j]);
else printf("%d ",b[i][j]);
}
}
return 0;
}
运行结果:
2
1 2
1 2
1 1
2 2
Process returned 0 (0x0) execution time : 10.968 s
Press any key to continue.