【題解】ZeroJudge d481: 矩陣乘法

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d481
【解題想法】

  • 矩陣 C = A * B,C[i][j] = Sum(A[i][k] * B[k][j]), 0<= k <= b
    • Dimensions of 矩陣 A: a x b
    • Dimensions of 矩陣 B: b x d
    • Dimensions of 矩陣 C: b x d
  • (Line-11/22):要宣告long long
#include <iostream>
using namespace std;
 
int main() {
    int a, b, c, d;
    while (cin >> a >> b >> c >> d){
        if (b != c){
            cout << "Error\n";
            continue;
        }
        long long A[a][b], B[b][d];
        for (int i = 0; i < a; i++){
            for (int j = 0; j < b; j++){
                cin >> A[i][j];
            }
        }
        for (int i = 0; i < c; i++){
            for (int j = 0; j < d; j++){
                cin >> B[i][j];
            }
        }
        long long C[a][d];
        for (int i = 0; i < a; i++){
            for (int j = 0; j < d; j++){
                C[i][j] = 0;
                for (int k = 0; k < c; k++){
                    C[i][j] += A[i][k] * B[k][j];
                }
            }
        }
        for (int i = 0; i < a; i++){
            for (int j = 0; j < d; j++){
                cout << C[i][j] << " ";
            }
            cout << "\n";
        }
    }
    return 0;
}

分享本文 Share with friends