天天看點

Bailian3256 矩陣的乘法【數學計算】

Bailian3256 矩陣的乘法

問題簡述:(略)

問題分析:

    簡單的計算問題,不解釋。

    計算矩陣c時,是可以邊計算邊輸出的。

程式說明:(略)

參考連結:(略)

題記:程式中,把計算邏輯和輸入輸出邏輯分開,多數情況是合理的。這樣做也有可能造成代碼不夠簡潔。

AC的C++語言程式如下:

/* Bailian3256 矩陣的乘法 */

#include <iostream>
#include <iomanip>

using namespace std;

const int N = 100;
int a[N][N], b[N][N], c[N][N];

void print(int a[][N], int x, int y)
{
    for(int i = 0; i < y; i++) {
        for(int j = 0; j < x; j++)
            cout << setw(5) << a[j][i];
        cout << endl;
    }
}

int main()
{
    int x1, x2, y1, y2;

    cin >> x1 >> y1;
    for(int i = 0; i < x1; i++)
        for(int j = 0; j < y1; j++)
            cin >> a[i][j];
    cin >> x2 >> y2;
    for(int i = 0; i < x2; i++)
        for(int j = 0; j < y2; j++)
            cin >> b[i][j];

    for(int i = 0; i < x1; i++)
        for(int j = 0; j < y2; j++) {
            c[i][j] = 0;
            for(int k = 0; k < x2; k++)
                c[i][j] += a[i][k] * b[k][j];
        }

    for(int i = 0; i < x1; i++) {
        for(int j = 0; j < y2; j++)
            cout << setw(5) << c[i][j];
        cout << endl;
    }

    return 0;
}
           

繼續閱讀