5 | 10 | 5 |
0 | 2 | 1 |
1 | 3 | 9 |
2 | 5 | 2 |
3 | 4 | 3 |
4 | 7 | 6 |
#include <stdio.h>
int main (void)
{
int i;
int j;
int k = 1;
int spar_max [5][10] = { /*定义稀疏矩阵*/
0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 9, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
0, 0, 0, 0, 0, 0, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 5, 0, 0 };
int com_max [6][3];
com_max [0][0] = 5; /*被压缩数组行数*/
com_max [0][1] = 10; /*被压缩数组列数*/
com_max [0][2] = 5; /*被压缩数组元素个数*/
for (i = 0; i < 5; i++) {
for (j = 0; j < 10; j++) {
if (spar_max [i][j] != 0) {
com_max [k][0] = i;
com_max [k][1] = j;
com_max [k][2] = spar_max [i][j];
}
}
k += 1;
}
/*输出压缩后的数组*/
for (i = 0; i < 6; i++) {
for (j = 0; j < 3; j++)
printf ("%-4d", com_max [i][j]);
printf("\n");
}
return (0);
}
beyes@linux-beyes:~/C/structer> ./sparse_matrices.exe
5 10 5
0 2 1
1 4 9
2 7 2
3 6 3
4 7 5
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |