3.3.3.1对称矩阵

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    
int Array2[4][4]=
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{1,2,4,7},
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{2,3,5,8},
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{4,5,6,9},
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{7,8,9,10}
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
InBlock.gif    
int Array1[4*(1+4)/2];
InBlock.gif    
//显示二维数组
InBlock.gif
    ShowArray2(&Array2[0][0],4,4);
InBlock.gif    
//将二维数组(对称矩阵)压缩存储到一维数组中
InBlock.gif
    int index=0;
InBlock.gif    
for(int i=0;i<4;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for(int j=0;j<=i;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Array1[index
++]=Array2[i][j];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
//显示一维数组的内容
InBlock.gif
    ShowArray1(Array1,sizeof(Array1)/sizeof(int));
InBlock.gif    cout
<<endl<<endl;
InBlock.gif
InBlock.gif    
int tempArray2[4][4];
InBlock.gif    
//通过一维数组得到二维数组(对称矩阵)
InBlock.gif
    for(int s=1;s<=4;s++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for(int d=1;d<=4;d++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int k=0;
InBlock.gif            
if (s>=d)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                k
=s*(s-1)/2+d;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                k
=d*(d-1)/2+s;
ExpandedSubBlockEnd.gif            }

InBlock.gif            tempArray2[s
-1][d-1]=Array1[k-1];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.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/801382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值