数据结构实验之数组三:快速转置
Time Limit: 1000MS Memory Limit: 65536KB
ProblemDescription
转置运算是一种最简单的矩阵运算,对于一个m*n的矩阵M( 1 =< m < = 10000,1 = < n < = 10000 ),它的转置矩阵T是一个n*m的矩阵,且T( i , j )=M( j , i )。显然,一个稀疏矩阵的转置仍然是稀疏矩阵。你的任务是对给定一个m*n的稀疏矩阵( m , n< = 10000 ),求该矩阵的转置矩阵并输出。矩阵M和转置后的矩阵T如下图示例所示。
稀疏矩阵M 稀疏矩阵T
Input
连续输入多组数据,每组数据的第一行是三个整数mu, nu, tu(tu <= 50),分别表示稀疏矩阵的行数、列数和矩阵中非零元素的个数,随后tu行输入稀疏矩阵的非零元素所在的行、列值和非零元素的值,同一行数据之间用空格间隔。(矩阵以行序为主序)
Output
输出转置后的稀疏矩阵的三元组顺序表表示。
ExampleInput
3 5 5
1 2 14
1 5 -5
2 2 -7
3 1 36
3 4 28
ExampleOutput
1 3 36
2 1 14
2 2 -7
4 3 28
5 1 -5
Hint
Author
xam
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1010000
struct node
{
int a, b, c;
}p[11000];
int cmp(const void *a, const void *b)
{
struct node *aa=(struct node *)a;
struct node *bb=(struct node *)b;
if(aa->a<bb->a)return aa->a-bb->a;
else if(aa->a==bb->a)return aa->b-bb->b;
}
int main()
{
int m, n, t;
while(~scanf("%d%d%d", &m, &n, &t))
{
for(int a=0; a<t; a++)
{
scanf("%d%d%d", &p[a].b, &p[a].a, &p[a].c);
}
qsort(p, t, sizeof(p[0]), cmp);
for(int a=0; a<t; a++)
printf("%d %d %d\n", p[a].a, p[a].b, p[a].c);
}
return 0;
}
/***************************************************
User name: jk160508孙振强
Result: Accepted
Take time: 0ms
Take Memory: 108KB
Submit time: 2017-01-16 21:16:30
****************************************************/
/***************************************************
User name: jk160505徐红博
Result: Accepted
Take time: 0ms
Take Memory: 108KB
Submit time: 2017-02-10 20:41:22
****************************************************/