<代码收藏>矩阵相乘

5 篇文章 0 订阅
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

void main()
{
srand(unsigned(time(NULL)));
int row=rand()%10;
int column=rand()%10;
int three=rand()%10;

int** p1=new int*[row];
for(int i=0;i<row;++i)
{
p1[i]=new int[column];
}
for(int i=0;i<row;++i)
{
for(int j=0;j<column;++j)
{
p1[i][j]=rand()%10;
}
}
cout<<"p1 is"<<endl;
for(int i=0;i<row;++i)
{
for(int j=0;j<column;++j)
{
cout<<p1[i][j]<<"\t";
}
cout<<endl;
}

/
int** p2=new int*[column];
for(int i=0;i<column;++i)
{
p2[i]=new int[three];
}
for(int i=0;i<column;++i)
{
for(int j=0;j<three;++j)
{
p2[i][j]=rand()%10;
}
}
cout<<endl;
cout<<"p2 is"<<endl;
for(int i=0;i<column;++i)
{
for(int j=0;j<three;++j)
{
cout<<p2[i][j]<<"\t";
}
cout<<endl;
}

int** p3=new int*[row];
for(int i=0;i<row;++i)
{
p3[i]=new int[three];
}
//初始化p3
for(int i=0;i<row;++i)
{
for(int j=0;j<three;++j)
{
p3[i][j]=0;
}
}

for(int i=0;i<row;i++)
{
for(int j=0;j<three;j++)
{
for(int k=0;k<column;k++)
p3[i][j]=p3[i][j]+p1[i][k]*p2[k][j];
}
}
cout<<"p3 is"<<endl;
for(int i=0;i<row;++i)
{
for(int j=0;j<three;++j)
{
cout<<p3[i][j]<<"\t";
}
cout<<endl;
}

//delete p1
for(int i=0;i<row;++i)
{
delete []p1[i];
}
delete []p1;

//delete p2
for(int i=0;i<column;++i)
{
delete []p2[i];
}
delete []p2;
//delete p3
for(int i=0;i<row;++i)
{
delete []p3[i];
}
int aa;
cin>>aa;
}

• 0
点赞
• 1
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

03-25 693

08-29 124
06-26 8311
08-23 1217
03-31 380
08-28 320
06-07 2875
03-10 3540
04-20 2万+
07-30 2442
09-02 363
02-19 110
05-17 133
03-23 225
02-13 239
12-18 482