- <pre name="code" class="cpp">/*
- Copyright (c)2017,烟台大学计算机与控制工程学院
- All rights reserved.
- 文件名称:第8周(2)项目2 - 对称矩阵压缩存储的实现与应用(2).cpp
- 作 者:姬长水
- 完成日期:2017年11月9日
- 版 本 号:v1.0
- 问题描述: (2)设计算法,实现两个用压缩形式存储的对称矩阵A和B的加法和乘法。实现中请使用好前面设计的基本运算。
- 输入描述:a,b对称矩阵的全部数据。
- 程序输出:矩阵a,b,a+b与a*b的结果。
- */
- </pre><br>
- <br>
- <pre></pre>
- <pre name="code" class="cpp">#include <iostream>
- #include <cstdio>
- using namespace std;
- #define N 4
- #define M 10
- int value(int a[],int i,int j)
- {
- if(i>=j)
- return a[(i*(i+1))/2+j];
- else
- return a[(j*(j+1))/2+i];
- }
- void madd(int a[],int b[],int c[][N])
- {
- int i,j;
- for(i=0;i<N;i++)
- for(j=0;j<N;j++)
- c[i][j]=value(a,i,j)+value(b,i,j);
- }
- void mult(int a[],int b[],int c[][N])
- {
- int i,j,k,s;
- for(i=0;i<N;i++)
- for(j=0;j<N;j++)
- {
- s=0;
- for(k=0;k<N;k++)
- s=s+value(a,i,k)*value(b,k,j);
- c[i][j]=s;
- }
- }
- void disp1(int a[])
- {
- int i,j;
- for(i=0;i<N;i++)
- {
- for(j=0;j<N;j++)
- printf("%4d",value(a,i,j));
- printf("\n");
- }
- }
- void disp2(int c[][N])
- {
- int i,j;
- for(i=0;i<N;i++)
- {
- for(j=0;j<N;j++)
- printf("%4d",c[i][j]);
- printf("\n");
- }
- }
- int main()
- {
- int a[M]={1,2,3,4,5,6,7,8,9,0};
- int b[M]={0,1,2,3,4,5,6,7,8,9};
- int c1[N][N],c2[N][N];
- madd(a,b,c1);
- mult(a,b,c2);
- cout<<"a矩阵"<<endl;
- disp1(a);
- cout<<"b矩阵"<<endl;
- disp1(b);
- cout<<"a+b:"<<endl;
- disp2(c1);
- cout<<"a*b:"<<endl;
- disp2(c2);
- cout<<endl;
- return 0;
- }
- </pre><pre name="code" class="cpp"><img src="https://img-blog.csdn.net/20171109111955514?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcmVueXVhbnN1bg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
- </pre><br>
05-07
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交