sparse matrix transpose

Description

Outputs the transpose of the sparse matrix. (No more than 20 rows and columns)

Input

Enter two positive integers n and m in the first line, which represent the number of rows and columns of the matrix, respectively,
and then enter the matrix triplet, and
finally enter (0 0 0) to indicate the end of the input.

Output

The transposed matrix.

  • Sample Input 
    4 4
    1 1 1
    2 1 2
    3 2 3
    0 0 0
  • Sample Output
    1 1 1
    1 2 2
    2 3 3
  • code:
#include<stdio.h>  
#include<stdlib.h>  
  
#define MAXSIZE 100  
  
typedef struct {  
  int  i,j;  
  you are;  
}Triple;  
  
typedef struct  {  
  Triple data[MAXSIZE+1];  
  int mu, no, you;  
} TSMatrix;  
void change(TSMatrix A)  
{  
    int m,k;  
    int c,r,n;  
    for(m = 0;m < A.tu-1;m++)//First ascending order  
    {  
        for(k = m;k < A.tu;k++)  
        {  
            if(A.data[m].j > A.data[k].j)  
            {  
                c = A.data[m].j;  
                A.data[m].j = A.data[k].j;  
                A.data[k].j = c;  
                r = A.data[m].i;  
                A.data[m].i = A.data[k].i;  
                A.data[k].i = r;  
                n = A.data[m].e;  
                A.data[m].e = A.data[k].e;  
                A.data[m].e = n;  
            }  
        }  
    }  
    for(m = 0;m < A.tu-1;m++)//The columns are the same, the rows are in ascending order  
    {  
        for(k = m;k < A.tu;k++)  
        {  
            if(A.data[m].j == A.data[k].j && A.data[m].i > A.data[k].i)  
            {  
                r = A.data[m].i;  
                A.data[m].i = A.data[k].i;  
                A.data[k].i = r;  
                n = A.data[m].e;  
                A.data[m].e = A.data[k].e;  
                A.data[k].e = n;  
            }  
        }  
    }  
}  
void output(TSMatrix A)  
{  
    int i;  
    for(i=0;i<A.tu;i++)  
    {  
        printf("%d %d %d\n",A.data[i].j,A.data[i].i,A.data[i].e);  
    }  
}  
intmain()  
{  
    TSMatrix A;  
    int n,m,i;  
    int x,y,z;  
    scanf("%d %d",&n,&m);  
    A.mu=n;  
    A.nu = m;  
    A.you=0;  
    for(i=0;;i++)  
    {  
        scanf("%d %d %d",&x,&y,&z);  
        if(x==0&&y==0&&z==0)  
            break;  
        A.data[i].i=x;  
        A.data[i].j=y;  
        A.data[i].e=z;  
        A.your++;  
    }  
    change(A);  
    output(A);  
    return 0;  
}  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644663&siteId=291194637