数据结构实验之数组三:快速转置
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
转置运算是一种最简单的矩阵运算,对于一个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
输出转置后的稀疏矩阵的三元组顺序表表示。
Example Input
3 5 51 2 141 5 -52 2 -73 1 363 4 28
Example Output
1 3 362 1 142 2 -74 3 285 1 -5
#include <iostream>
using namespace std;
struct node
{
int row;
int col;
int data;
}mp[10010];
void func(int n)
{
int i,j;
struct node t;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(mp[j].col>mp[j+1].col)
{
t=mp[j];
mp[j]=mp[j+1];
mp[j+1]=t;
}
else if(mp[j].col==mp[j+1].col)
{
if(mp[j].row>mp[j+1].row)
{
t=mp[j];
mp[j]=mp[j+1];
mp[j+1]=t;
}
}
}
}
}
int main()
{
int mu,nu,tu;
int i,j;
while(cin>>mu>>nu>>tu)
{
for(i=0;i<tu;i++)
cin>>mp[i].row>>mp[i].col>>mp[i].data;
func(tu);
for(i=0;i<tu;i++)
cout<<mp[i].col<<" "<<mp[i].row<<" "<<mp[i].data<<endl;
}
return 0;
}