#include <iostream>
using namespace std;
const int maxn=31;
int main()
{
int n,m;
unsigned long int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
cin>>n>>m;
int i,j,k,l;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
{
cin>>a[i][j];
b[i][j]=a[i][j];
c[i][j]=0;
}
if (m>0)//非零次幂的时候
{
for (l=0;l<m-1;l++)
{
for (i=0;i<n;i++)
for (j=0;j<n;j++)
for (k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];//求(AB)ij
for (i=0;i<n;i++)
for (j=0;j<n;j++)
{
b[i][j]=c[i][j]; //把结果变成乘数,作为下一次使用
c[i][j]=0;
}
}
//---------------------显示---------------------
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
cout<<b[i][j]<<' ';
cout<<endl;
}
}
else //零次幂的时候,会结果是单位矩阵
{
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
if (i==j) cout<<1<<' ';
else cout<<0<<' ';
cout<<endl;
}
}
return 0;
}
矩阵乘法
最新推荐文章于 2024-06-28 08:35:53 发布