03-稀疏矩阵
来源:互联网 发布:大学生linux基础知识 编辑:程序博客网 时间:2024/05/19 18:42
二维数组表格内容
为了提高内存使用效率,压缩表示
压缩,是将有效的数据保存下来,上述中无效的数据直接进行了抛弃,而现实中,往往会将重复的数据视为一个有效数据存储,在上述结构中稍作修改即可实现。
#include <iostream>
using namespace std;
void printDepress(int arr[][3])
{
cout<<"------"
<<arr[0][0]<<" "
<<arr[0][1]<<" "
<<arr[0][2]<<"----"<<endl;
for (int i=1; i<=arr[0][2]; ++i) {
for (int j=0; j<3; ++j) {
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{
int data[5][10] = {
0,0,1,0,0,0,0,0,0,0,
0,0,0,9,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,0,0,6,0,0
};
int count = 0;
int depress[20][3];
depress[0][0] = 5;
depress[0][1] = 10;
for (int row=0; row<5; ++row) {
for (int col=0; col<10; ++col) {
if (data[row][col] != 0) {
++count;
depress[count][0] = row;
depress[count][1] = col;
depress[count][2] = data[row][col];
}
}
}
// 有效数据个数
depress[0][2] = count;
printDepress(depress);
return 0;
}
结果:
------5 10 5----
0 2 1
1 3 9
2 5 2
3 4 3
4 7 6
Program ended with exit code: 0
- 03-稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- 稀疏矩阵
- C语言实现字节流与十六进制字符串的相互转换
- MySQL逗号分割字段的行列转换技巧
- 使用系统的AVMetadataObject类实现二维码扫描
- 关于使用JSONKit一直崩溃
- Java 构造方法,初始化块,静态初始化块
- 03-稀疏矩阵
- 阅读《Android 从入门到精通》(13)——日期选择器
- 小米的神奇是不可复制的,但营销还是独到的!
- postgresql数据库的安装一(源码编译)
- Bootstrap警告
- 常用加密算法的Java实现总结
- IAR 扩展关键字
- poj 2236 Wireless Network(并查集)
- GDAL/ORG 支持的格式