3.3.3.2上三角矩阵

None.gif //  DataStructTest.cpp : Defines the entry point for the console application.
None.gif
//
None.gif

None.gif#include 
" stdafx.h "
None.gif#include 
< iostream.h >
None.gif#include 
< malloc.h >
None.gif
None.gif
void  ShowArray2( int   *  p, int  row, int  col)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int index=0;
InBlock.gif    
for(int i=0;i<row;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for(int j=0;j<col;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (j==0)
InBlock.gif                cout
<<p[index++];
InBlock.gif            
else
InBlock.gif                cout
<<" "<<p[index++];
ExpandedSubBlockEnd.gif        }

InBlock.gif        cout
<<endl;
ExpandedSubBlockEnd.gif    }

InBlock.gif    cout
<<endl;
ExpandedBlockEnd.gif}

None.gif
None.gif
void  ShowArray1( int   *  p, int  count)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
for(int k=0;k<count;k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (k==0)
InBlock.gif            cout
<<p[k];
InBlock.gif        
else
InBlock.gif            cout
<<" "<<p[k];
ExpandedSubBlockEnd.gif    }

InBlock.gif    cout
<<endl;
ExpandedBlockEnd.gif}

None.gif
None.gif
int  main( int  argc,  char *  argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
const int N=4;
InBlock.gif    
int Array2[N][N]=
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{1,2,3,4},
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{0,5,6,7},
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{0,0,8,9},
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{0,0,0,10},
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
InBlock.gif    
int Array1[N*(1+N)/2+1];
InBlock.gif    
//显示二维数组
InBlock.gif
    ShowArray2(&Array2[0][0],4,4);
InBlock.gif    
//将二维数组(上三角矩阵)压缩存储到一维数组中
InBlock.gif
    int index=0;
InBlock.gif    
InBlock.gif    
for(int i=1;i<=N;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//N-i+1为第i行应该存储的元素数
InBlock.gif
        for(int j=1;j<=N-i+1;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Array1[index
++]=Array2[i-1][i+j-2];                //i+j-2=N-(N-i+1)+(j-1)    //表示总阶数-本行应该存储的元素数+本行的J索引
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    Array1[N
*(1+N)/2]=0;                                    //在一维数组的最末元素存储三角矩阵的常数
InBlock.gif    
//显示一维数组的内容
InBlock.gif
    ShowArray1(Array1,sizeof(Array1)/sizeof(int));
InBlock.gif    cout
<<endl<<endl;
InBlock.gif    
//通过一维数组得到二维数组(上三角矩阵)
InBlock.gif
    int tempArray2[N][N];
InBlock.gif    
for(int j=1;j<=N;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for(int k=1;k<=N;k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int index=0;
InBlock.gif            
if (j<=k)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//这里必需指明:(float)(j-1),将j-1转为浮点型,否则做(j-1)/2时将被认为要(j-1)/2存到整型里,使精度降低,
InBlock.gif                
//再后续的操作就是错的了.加上类型转化后,后续的操作编译器将把所有的变量以及常量提升到float后再运算.
InBlock.gif                
//运算的结果是float的,所以还需要再进行一次类型转换,消除编译器警告(不转也正确,因为会自动的使float变成int).
InBlock.gif
                index=(int)((float)(j-1)/2*(2*N-j+2)+k-j+1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                index
=N*(1+N)/2+1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            tempArray2[j
-1][k-1]=Array1[index-1];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
//
InBlock.gif
    ShowArray2(&tempArray2[0][0],4,4);
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/fxwdl/archive/2007/06/30/801384.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值