A: Matrix Transpose
A
matrix is a rectangular array of elements, most commonly numbers. A matrix with
rows and
columns is said to be an
-by-
matrix. For example,
A: Matrix Transpose |
is a 4-by-3 matrix of integers.
The individual elements of a matrix are usually given lowercase symbols and are distinguished by subscripts. The th row and th column of matrix is usually referred to as . For example, . Matrix subscripts are 1-based.
The transpose of a matrix , denoted , is formed by interchanging the rows and columns of . That is, the th element of is the th element of . For example, the transpose of matrix above is:
A matrix is said to be sparse if there are relatively few non-zero elements. As a -by- matrix has number of elements, storing all elements of a large sparse matrix may be inefficient as there would be many zeroes. There are a number of ways to represent sparse matrices, but essentially they are all the same: store only the non-zero elements of the matrix along with their row and column.
You are to write a program to output the transpose of a sparse matrix of integers.
Input
You are given several sparse matrix in a row, each of them described as follows. The first line of the input corresponds to the dimension of the matrix, and (which are the number of rows and columns, respectively, of the matrix). You are then given sets of numbers, which represent the rows of the matrix. Each set consists of two lines which represents a row of the matrix. The first line of a set starts with the number , which is the number of non-zero elements in that row, followed by numbers which correspond to the column indices of the non-zero elements in that row, in ascending order; the second line has integers which are the matrix elements of that row. For example, matrix above would have the following representation:4 3 3 1 2 3 1 3 2 2 2 3 4 -1 0 3 1 2 3 5 -2 11Note that for a row with all zero elements, the corresponding set would just be one number, ` 0', in the first line, followed by a blank line.
You may assume:
- the dimension of the sparse matrix would not exceed 10000-by-10000,
- the number of non-zero element would be no more than 1000,
- each element of the matrix would be in the range of -10000 to 10000, and
- each line has no more than 79 characters.
Output
For each input case, the transpose of the given matrix in the same representation.Sample Input
4 3 3 1 2 3 1 3 2 2 2 3 4 -1 0 3 1 2 3 5 -2 11
Sample Output
3 4 2 1 4 1 5 3 1 2 4 3 4 -2 3 1 2 4 2 -1 11
#include<stdio.h>
#include<string.h>
int ans[1007];
int z[1007][1007],y[1007][1007],mat[1007][1007],tmat[1007][1007];
int main()
{
int m,n,i,j,a,b;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(mat,0,sizeof(mat));
for(i=0; i<m; i++)
{
int count;
scanf("%d",&count);
for(a=0; a<count; a++)
scanf("%d",&z[i][a]);
for(b=0; b<count; b++)
scanf("%d",&y[i][b]);
for(j=0; j<count; j++)
{
int t=z[i][j]-1;
mat[i][t]=y[i][j];
}
}
//记下转置矩阵
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
tmat[i][j]=mat[j][i];
}
}
printf("%d %d\n",n,m);
memset(ans,0,sizeof(ans));
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(tmat[i][j]!=0)
ans[i]++;//记下有几个非0的个数
}
}
int flag;
for(i=0; i<n; i++)
{
printf("%d",ans[i]);
if(ans[i]==0)
{
printf("\n\n");
continue;
}
for(j=0; j<m; j++)
{
if(tmat[i][j]!=0)
{
printf(" %d",j+1);
}
}
printf("\n");
for(int e=0; e<m; e++)
{
if(tmat[i][e]!=0)
{
printf("%d",tmat[i][e]);
flag=e;
break;//防止格式错误
}
}
for(int f=flag+1; f<m; f++)
{
if(tmat[i][f]!=0)
printf(" %d",tmat[i][f]);
}
printf("\n");
}
}
return 0;
}