#include <bits/stdc++.h>
#define Max 100
#define OK 1
using namespace std;
typedef int Elemtype;
typedef int Status;
typedef struct
{
int i, j;
Elemtype e;
}Triple;
typedef struct
{
Triple data[Max+1];
int mu, nu, tu;
}TSmatrix;
Status Transpose(TSmatrix M, TSmatrix &T)
{
T.mu = M.mu, T.nu = M.nu, T.tu = M.tu;
if(T.tu)
{
int q=1;
for(int col = 1; col <= M.nu; col ++)
{
for(int p = 1; p <= M.tu; p++)
if(M.data[p].j == col)
{
T.data[q].i = M.data[p].j;
T.data[q].j = M.data[p].i;
T.data[q].e = M.data[p].e;
++q;
}
}
}
return OK;
}
int main()
{
int mu, nu, tu;
while(~scanf("%d %d %d", &mu, &nu, &tu))
{
TSmatrix T, M;
M.mu = mu, M.nu = nu, M.tu = tu;
for(int p = 1; p <= tu; p ++)
{
int i, j, e;
scanf("%d %d %d", &i, &j, &e);
M.data[p].i = i;
M.data[p].j = j;
M.data[p].e = e;
}
Transpose(M, T);
for(int p = 1; p <= T.tu; p++)
{
printf("%d %d %d\n", T.data[p].i, T.data[p].j, T.data[p].e);
}
}
return 0;
}
快速转置:
#include <bits/stdc++.h>
#define Max 10000
#define OK 1
using namespace std;
typedef int Elemtype;
typedef int Status;
typedef struct
{
int i, j;
Elemtype e;
}Triple;
typedef struct
{
Triple data[Max+1];
int mu, nu, tu;
}TSmatrix;
Status Transpose(TSmatrix M, TSmatrix &T)
{
T.mu = M.mu, T.nu = M.nu, T.tu = M.tu;
Elemtype num[Max], cpot[Max];
if(T.tu)
{
for(int col = 1; col <= M.nu; col++)
{
num[col] = 0;
}
for(int t = 1; t <= M.tu; t++)
{
++num[M.data[t].j];
}
cpot[1] = 1;
for(int col = 2; col <= M.nu; col ++)
{
cpot[col] = cpot[col-1]+num[col-1];
}
for(int p = 1; p <= M.tu; p++)
{
int col = M.data[p].j;
int q=cpot[col];
T.data[q].i = M.data[p].j;
T.data[q].j = M.data[p].i;
T.data[q].e = M.data[p].e;
++cpot[col];
}
}
return OK;
}
int main()
{
int mu, nu, tu;
while(~scanf("%d %d %d", &mu, &nu, &tu))
{
TSmatrix T, M;
M.mu = mu, M.nu = nu, M.tu = tu;
for(int p = 1; p <= tu; p ++)
{
int i, j, e;
scanf("%d %d %d", &i, &j, &e);
M.data[p].i = i;
M.data[p].j = j;
M.data[p].e = e;
}
Transpose(M, T);
for(int p = 1; p <= T.tu; p++)
{
printf("%d %d %d\n", T.data[p].i, T.data[p].j, T.data[p].e);
}
}
return 0;
}