数据结构实验之数组三:快速转置
2017-10-21 09:35
567 查看
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 5 1 2 14 1 5 -5 2 2 -7 3 1 36 3 4 28 Example Output 1 3 36 2 1 14 2 2 -7 4 3 28 5 1 -5
代码一
#include<iostream> using namespace std; #include<cstdlib> #include<cstdio> #define Maxsize 10010 typedef int element; typedef struct { int i , j ; element e; }Triple; typedef struct { Triple data[Maxsize+1]; int mu , nu , tu; }TSMatrix; //转置 //行列值交换;i j 交换 ; 重排三元组的次序输出 void TransposeSMaTrix(TSMatrix M , TSMatrix &T) { T.mu = M.nu; T.nu = M.mu ;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; } } } } } //时间复杂度O(nu*mu) int main() { TSMatrix M ,T; while(cin >> M.mu >> M.nu >> M.tu) { for(int i = 1;i<=M.tu;i++) cin >> M.data[i].i >> M.data[i].j >> M.data[i].e; TransposeSMaTrix(M,T); for(int i = 1;i<=T.tu;i++) cout << T.data[i].i << " "<< T.data[i].j << " " << T.data[i].e << endl; } return 0; }
代码二
num[ ] 表示原三元组M第col列非零元的个数,即新三元组T第col行非零元个数
cpot[col] 表示M第col列的第一个非零元在T中的位置
如M T
1 2 14 1 3 36
1 5 -5 2 1 14
2 2 -7 2 2 -7
3 1 36 4 3 28
3 4 28 5 1 -5
col 1 2 3 4 5 6 7
num[col] 2 2 2 1 0 1 0
cpot[col] 1 3 5 7 8 8 9
cpot[1] = 1
cpot[col] = cpot[col-1] + num[col-1] 2<= col <=M.nu
#include<iostream> using namespace std; #include<cstdlib> #include<cstdio> #define Maxsize 10010 int num[Maxsize],cpot[Maxsize]; typedef int element; typedef struct { int i , j ; element e; }Triple; typedef struct { Triple data[Maxsize+1]; int mu , nu , tu; }TSMatrix; void Fast(TSMatrix M , TSMatrix &T) { T.mu = M.nu; T.nu = M.mu ; T.tu = M.tu; if(T.tu) { for(int col = 0;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]; } } }//时间复杂度是O(nu+tu) int main() { TSMatrix M ,T; while(cin >> M.mu >> M.nu >> M.tu) { for(int i = 1;i<=M.tu;i++) cin >> M.data[i].i >> M.data[i].j >> M.data[i].e; Fast(M,T); for(int i = 1;i<=T.tu;i++) cout << T.data[i].i << " "<< T.data[i].j << " " << T.data[i].e << endl; } return 0; }
相关文章推荐
- 数据结构实验之数组三:快速转置
- SDUT 3347 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- sdutacm-数据结构实验之数组三:快速转置
- SDUT 3347 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 3347 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置
- SDUTOJ(3347) 数据结构实验之数组三:快速转置
- 3347-数据结构实验之数组三:快速转置
- 数据结构实验之数组三:快速转置