3095. 矩阵乘法

2694994135
2694994135
Fifnmar

简单写了一下矩阵乘法,代码有点丑:

#include <cstdio>
#include <cstring>
using namespace std;

inline int get_int() {
    int ans = 0;
    bool fuvu = false;
    signed char ch = getchar();
    while (ch < '0' || '9' < ch) {
        if (ch == '-')
            fuvu = true;
        ch = getchar();
    }
    while ('0' <= ch and ch <= '9') {
        ans = ans * 10 + ch - '0';
        ch = getchar();
    }
    return fuvu ? -ans : ans;
}

inline int **multiply(int **a, int **b, int n) {
    int **c = new int *[n];
    for (int i = 0; i < n; ++i) {
        c[i] = new int[n];
        memset(c[i], 0, sizeof(int) * n);
        for (int j = 0; j < n; ++j)
            for (int k = 0; k < n; ++k)
                c[i][j] += a[i][k] * b[k][j];
    }
    return c;
}

int main() {
    int n = get_int();
    int **a = new int *[n], **b = new int *[n];
    for (int i = 0; i < n; ++i) {
        a[i] = new int[n];
        for (int j = 0; j < n; ++j)
            a[i][j] = get_int();
    }
    for (int i = 0; i < n; ++i) {
        b[i] = new int[n];
        for (int j = 0; j < n; ++j)
            b[i][j] = get_int();
    }
    int **c = multiply(a, b, n);
    for (int i = 0; i < n; ++i) {
        printf("%i", c[i][0]);
        for (int j = 1; j < n; ++j)
            printf(" %i", c[i][j]);
        printf("\n");
    }
}
你当前正在回复 博客/题目
存在问题!