现在的位置: 首页 > 综合 > 正文

对称矩阵压缩存储

2013年11月05日 ⁄ 综合 ⁄ 共 622字 ⁄ 字号 评论关闭

 

#include<iostream>
using namespace std;

int main()
{
  int   a[5][5]   =   {{1,   0,   2,   4,   3},   {0,   1,   3,   0,   2}, 
    {2,   3,   1,   0,   0},   {4,   0,   0,   7,   8},   {3,   2,   0,   8,   0}};

  int   b[25] ;
  int   i,   j,   k,  t,   r;
 for(i = 0;   i < 5;   i++)
  {
    for(j = 0;   j < 5;   j++)
      cout <<a[i][j] << "  ";
    cout <<endl<<endl;
    cout <<endl;
  }
  cout <<endl;
  k=0;
  for( i = 0; i < 5; i++ )
   for( j = 0; j <= i; j++ )
   {
    if( i >= j )
    {
     k=i*(i+1)/2+j;
    }
    else
    {
     k=j*(j+1)/2+i;
    }
    b[k++]=a[i][j];
   
   }
   for(t = 0;   t < k;   t++)
   {
      if   (t%5   ==   0)
   {
         cout <<endl<<endl;
         cout <<endl;
   }
         cout <<b[t] << "   ";
   }  
     cout <<endl;
 
  return   0;
}

抱歉!评论已关闭.