Problem Description
数组——矩阵的转置
给定一个m*n的矩阵(m,n<=100),求该矩阵的转置矩阵并输出。
Input
输入包含多组测试数据,每组测试数据格式如下:
第一行包含两个数m,n
以下m行,每行n个数,分别代表矩阵内的元素。
(保证矩阵内的数字在int范围之内)
Output
对于每组输出,输出给定矩阵的转置矩阵。两组输出之间用空行隔开。
Example Input
2 3 1 2 3 4 5 6 1 1 1
Example Output
1 4 2 5 3 6 1
Hint
#include<stdio.h>
#include<stdlib.h>
#define maxsize 10000
struct point
{
int r;
int c;
int data;
};
struct hh
{
int rows;
int nums;
int cols;
struct point p[maxsize];
};
int main()
{
int i,j,t;
struct hh *img;
img=(struct hh *)malloc(sizeof(struct hh));
while(scanf("%d%d",&img->rows,&img->cols)!=EOF)
{
t=0;
img->nums=img->rows*img->cols;
for(i=0;i<img->rows;i++)
{
for(j=0;j<img->cols;j++)
{
scanf("%d",&img->p[t].data);
img->p[t].r=i;
img->p[t].c=j;
t++;
}
}
t=img->cols;
img->cols=img->rows;
img->rows=t;
for(i=0;i<img->nums;i++)
{
t=img->p[i].c;
img->p[i].c=img->p[i].r;
img->p[i].r=t;
}
for(i=0;i<img->rows;i++)
{
for(j=0;j<img->cols;j++)
{
for(t=0;t<img->nums;t++)
{
if(img->p[t].r==i&&img->p[t].c==j)
{
if(j<img->cols-1)
printf("%d ",img->p[t].data);
else if(j==img->cols-1)
printf("%d\n",img->p[t].data);
}
}
}
}
printf("\n");
}
return 0;
}