#include <iostream>
#include <vector>
using namespace std;
int main()
{
int m,n,k;
cout<<"A陣列->想要幾個列的二維陣列元素:" ;
cin>>m;
cout<<"A陣列->想要幾個行的二維陣列元素:" ;
cin>>n;
cout<<"B陣列->想要幾個行的二維陣列元素:" ; //B陣列沒有幾列是因為必須要與A陣列的行數一樣才能相乘
cin>>k;
vector <vector <int> > a(m,vector<int> (n));
vector <vector <int> > b(n,vector<int> (k));
vector <vector <int> > c(m,vector<int> (k));
cout<<"輸入A陣列的值:";
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cin>>a[i][j];
}
cout<<endl;
cout<<"輸入B陣列的值:";
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++)
cin>>b[i][j];
}
cout<<endl;
cout<<"相乘後:"<<endl;
for(int i=0;i<m;i++) //列
{
for(int j=0;j<k;j++) //每列的個數值
{ c[i][j]=0; //C的每一個數皆歸零
for(int h=0;h<n;h++)
{
c[i][j]=c[i][j]+a[i][h]*b[h][j];
}
cout<<c[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
system("pause");
return 0;
}
全站熱搜