数据结构C语言>数组>压缩二维数组

二维数组里,有大部分空间没使用,为了增加数组内存的使用效率,我们要压缩它。

嗯,上代码

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6   int sparse[5][10]=    {0010000000,
 7                          0009000000,
 8                          0000020000,
 9                          0000300000,
10                          0000000600 };//稀疏矩阵 
11   int compress[6][3]; //压缩数组 
12   int i,j,k;
13   k=1;
14   compress[0][0= 5;//数组sparse有5行 
15   compress[0][1= 10;//数组sparse 有10列 
16   compress[0][2= 5//数组sparse有5个元素 
17   for(i=0; i<5; i++//二维数组遍历 
18   {
19     for(j=0; j<10; j++)
20     {
21        if(sparse[i][j] != 0//元素没被使用 
22        {
23          compress[k][0= i;//存储行数 
24          compress[k][1= j;//存储列数 
25          compress[k][2= sparse[i][j];//存储元素值 
26          k++;//下一行 
27        }      
28     }       
29   }
30   for(i=0; i<6; i++)  //压缩数组输出 
31   {
32     for(j=0; j<3; j++)
33     {printf("%3d",compress[i][j]);}
34     printf("\n");
35   }
36                          
37                          
38   system("PAUSE");    
39   return 0;
40 }
41 

 

 

posted @ 2010-08-17 23:19  草珊瑚  阅读(2535)  评论(0编辑  收藏  举报