L1-048. 矩阵A乘以B

水题不多解释
#include<iostream>
#include<string.h>
using namespace std;
int main()  {
	int n1, m1, n2, m2;
	cin>>n1>>m1;
	int ju1[n1][m1];
	for(int i = 0; i < n1; i++)
	    for(int j = 0; j < m1; j++)
	        cin>>ju1[i][j];
	cin>>n2>>m2;
	int ju2[n2][m2];
	if(m1 != n2)
	    cout<<"Error: "<<m1<<" != "<<n2;
	else  {
		for(int i = 0; i < n2; i++)
		    for(int j = 0; j < m2; j++)  
		    	cin>>ju2[i][j];
	    int zu[n1][m2];
	    memset(zu, 0, sizeof(zu));
	    cout<<n1<<" "<<m2<<endl;
	    for(int i = 0; i < n1; i++)  {
		    for(int j = 0; j < m2; j++)  {
			    for(int k = 0; k < m1; k++)
			        zu[i][j] += ju1[i][k] * ju2[k][j];
			    if(j == 0)
			        cout<<zu[i][j];
		    	else
		    	    cout<<" "<<zu[i][j]; 
		    }
	    	cout<<endl;
    	}
	}
} 


阅读更多

没有更多推荐了,返回首页