1357 Matrix Multiplication

Source: Internet
Author: User
Description

Given two matrices A (N * t) and B (T * m), calculate c = a * B.

Input

The first line contains an integer T, indicating that there are T groups of test data.

The format of each group of test data is as follows:

The first three positive integers (n t m) represent the N * t of matrix A and the T * m of matrix B respectively, both smaller than or equal to 100.

The next n rows have t integers in each line and describe matrix;

Next we have t rows. Each row has m integers, which describe matrix B.

The value range is [-100,100].

Output

Output N * m row matrix C

 

 

For classic problems, follow the steps above in linear algebra.

# Include <stdio. h> # include <stdlib. h> // B [J] [k] * C [k] [I] = A [J] [I] void matrix (INT ** B, int ** C, int ** A, int NX, int NY, int NK) {int I, j, k; For (j = 0; j <NY; j ++) for (I = 0; I <NX; I ++) A [J] [I] = 0; For (j = 0; j <NY; j ++) {for (I = 0; I <NX; I ++) {for (k = 0; k <NK; k ++) A [J] [I] + = B [J] [k] * C [k] [I] ;};} void main () {int I, j, k, TMP; int B _row, B _col; int c_row, c_col; int a_row, a_col; int ** B, ** C, ** A; int number, Te; int N, t, M; scanf ("% d", & number); For (TE = 1; te <= number; Te ++) {scanf ("% d", & N, & T, & M); // enter the number of columns in array B, B _row = N; B _col = T; c_row = T; c_col = m; // enter the number of columns in the C array a_row = B _row; a_col = c_col; A = (INT **) malloc (sizeof (int *) * a_row ); for (j = 0; j <a_row; j ++) {A [J] = (int *) malloc (sizeof (INT) * a_col );} B = (INT **) malloc (sizeof (int *) * B _row); For (j = 0; j <B _row; j ++) {B [J] = (int *) malloc (sizeof (INT) * B _col);} c = (INT **) malloc (sizeof (int *) * c_row ); for (j = 0; j <c_row; j ++) {C [J] = (int *) malloc (sizeof (INT) * c_col );} // input B array element for (j = 0; j <B _row; j ++) for (I = 0; I <B _col; I ++) {scanf ("% d", & TMP); B [J] [I] = TMP;} // enter the C-array element for (j = 0; j <c_row; j ++) for (I = 0; I <c_col; I ++) {scanf ("% d", & TMP ); c [J] [I] = TMP;} matrix (B, C, A, a_col, a_row, B _col); For (j = 0; j <a_row; j ++) {for (I = 0; I <a_col; I ++) printf ("% d", a [J] [I]); printf ("\ n ");}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.