c++基础_矩阵乘法

#include <iostream> 
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    long c[a][a],c1[a][a],c2[a][a],temp[a][a];
    for(int i=0;i<a;i++){
        for(int j=0;j<a;j++){
            cin>>c[i][j];
            c1[i][j]=c[i][j];
            c2[i][j]=c[i][j];
            temp[i][j]=c[i][j];
        }
    }

    for(int cishu=1;cishu<b;cishu++){
    for(int i=0;i<a;i++){
        for(int j=0;j<a;j++){
            
            c1[i][j]=temp[i][j];
        }
    }

        
        for(int i=0;i<a;i++){
            for(int j=0;j<a;j++){
                long total=0;
                for(int k=0,m=0;k<a,m<a;k++,m++){
                    
                    total+=c1[i][k]*c2[m][j];
                    
                }
                temp[i][j]=total;
//                cout<<"t:"<<total<<endl;
                
            }
        }
        
    }
    
    for(int i=0;i<a;i++){
        for(int j=0;j<a;j++){
            cout<<temp[i][j]<<" ";
        }
        cout<<endl;
    }
}

 

posted on 2018-03-26 22:25  我吃你大西瓜  阅读(416)  评论(0编辑  收藏  举报

导航