数据结构—矩阵课后题

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

P-219-29T

template

T** LowerMatrix::operator*(const LowerMatrix& m) const{ if(n!=m.n) throw SizeMismatch();

T** w = new T *[n];

for(int i=0;i

w[i] = new int[n];

}

int ct=0,cm=0;

for(int i=1;i<=n;i++){

for(int j=1;j<=n;j++){

T sum = t[ct]*m.t[cm];

for(int k=j;k

ct++;

cm++;

sum += t[ct]*m.t[cm];

}

w[i-1][j-1]=sum;

if(i

w[i-1][j-1] = 0;

}

ct = i*(i-1)/2+j-1;

cm = j;

}

ct = i*(i+1)/2;

cm = 0;

}

return w;

}

函数时间复杂性为O(n^3);

P-219-30T

template

T** UpperMatrix::operator*(const LowerMatrix& m) const{ int front=0;

if(n!=m.n) throw SizeMismatch();

T** c = new T *[n];

for(int i=0;i

c[i] = new int[n];

}

int ct=0,cm=0;

for(int i=0;i

for(int j=0;j

c[i][j]=0;

if(i<=j)front=j;

else front=i;

for(int k=front;k

ct = i*(2*n-i)/2+k-i;

cm = k*(k+1)/2+j;

c[i][j] += t[ct]*m.t[cm];

}

}

}

return c;

}

函数时间复杂性为O(n^3);

P-237-36T

template

SparseMatrix& SparseMatrix::Store(const int& x,int i,int j){ if(i<1 || j<1 || i>rows || j>cols ) throw OutOfBounds();

if(terms = = 0){

if(x ==0 )

return *this;

a[0].row = i;

a[0].col = j;

a[0].value = x;

terms++;

return *this;

}

int location = a[0].row*cols + a[0].col;

int other = i*cols + j;

int k = 0;

while(klocation){

k++;

if(k!=terms)

location = a[k].row*cols + a[k].col;

}

if(k == terms){

if(terms = = MaxTerms) throw OutOfBounds();

a[k].row = i;

a[k].col = j;

a[k].value = x;

terms++;

}

if(other = = location){

a[k].row = i;

a[k].col = j;

a[k].value = x;

}

else{

if(terms = = MaxTerms) throw OutOfBounds();

for(int l=k;l

a[l+1] = a[l];

a[k].row = i;

a[k].col = j;

a[k].value = x;

terms++;

}

return *this;

}

template

T SparseMatrix::Retrieve(int i,int j) const{

if(i<1 || j<1 || i>rows || j>cols) throw OutOfBounds();

int location = a[0].row*cols + a[0].col;

int other = i*cols + j;

int k = 0;

while(klocation){

k++;

if(k!=terms)

location = a[k].row*cols + a[k].col;

}

if(other == location)

return a[k].value;

else

return 0;

}

Store函数和Retrieve 函数的时间复杂性均为O(terms)

P-237-43

template

SparseMatrix& SparseMatrix::Multiply(SparseMatrix& m,SparseMatrix& n){

if(cols!=m.rows) throw SizeMismatch();

相关文档
最新文档