下三角形数组转换为一维数组
来源:互联网 发布:南海大数据研究院 编辑:程序博客网 时间:2024/04/27 20:52
(1) 将大小为n*n的下三角数组转换成以行为主的一维数组,且不存储内容为0的元素!
data[i][j]的位置 =i*(i+1)/2+j;
(2) 将大小为n*n的下三角数组转换成以列为主的一维数组,且不存储内容为0的元素!
data[i][j]的位置=[n+(n-j+1)]*j/2+(i-j)
(1)
#include <iostream>
#include <iomanip>
using namespace std;
int Lower[5][5]={
3,0,0,0,0,
7,5,0,0,0,
6,4,5,0,0,
8,3,2,1,0,
9,1,6,4,9
};
int main()
{
int RowMajor[15];
int Index,i,j;
cout<<"原下三角数组:\n";
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
cout<<setw(3)<<Lower[i][j];
cout<<endl;
}
//进行以行为主的数组数据转换
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(i>=j)
{
Index=i*(i+1)/2+j;
RowMajor[Index]=Lower[i][j];
}
cout<<endl;
cout<<"转换成以行为主的一维数组:\n";
for(i=0;i<15;i++)
cout<<setw(3)<<RowMajor[i];
cout<<"\n\n";
return 0;
}
(2)
#include <iostream>
#include <iomanip>
using namespace std;
int Lower[5][5]={
3,0,0,0,0,
7,5,0,0,0,
6,4,5,0,0,
8,3,2,1,0,
9,1,6,4,9
};
int main()
{
int ColMajor[15];
int Index,i,j;
cout<<"原下三角数组:\n";
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
cout<<setw(3)<<Lower[i][j];
cout<<endl;
}
//进行以列为主的数组数据转换
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(i>=j)
{
Index=(11-j)*j/2+(i-j);
ColMajor[Index]=Lower[i][j];
}
cout<<endl;
cout<<"转换成以列为主的一维数组:\n";
for(i=0;i<15;i++)
cout<<setw(3)<<ColMajor[i];
cout<<"\n\n";
return 0;
}
- 下三角形数组转换为一维数组
- 上三角形数组转换为一维数组
- 多维数组转换为一维数组
- 一维List转换为二维数组
- python矩阵转换为一维数组
- python矩阵转换为一维数组
- 二维数组转换为一维
- 动态二维数组转换为一维数组
- Matlab中将多维数组转换为一维数组
- 数字三角形:顺推法(一维数组)
- js将字符串转换为一维二维数组
- js的多维数组转换为一维
- 二维数组转化为一维数组
- [基础]php递归将多维数组转换为一维数组
- Tricks(三十二)—— 二维数组转换为一维数组
- 字符串转换为数组
- 数组转换为String
- map转换为数组:
- python 学习笔记 3 -- 数据结构篇
- MyISAM 和InnoDB两种mysql引擎的对比
- 一个基于Mahout与hadoop的聚类搭建
- kvm虚拟机redhat安装oracle之更改rhel6 yum为centos yum
- Android NEON指令学习
- 下三角形数组转换为一维数组
- 关于哲学家进餐问题
- Html中的favicon.ico
- java 动态代理深度学习(Proxy,InvocationHandler),含$Proxy0源码(转)
- 游戏项目中对CCTableView的使用
- 本机ip、127.0.0.1和0.0.0.0区别(转载)
- Android源码目录结构详解
- XX教你玩 MiniPC (二)MK908升级包分析
- Cocoa框架