矩阵相乘
程序员文章站
2022-05-17 20:40:11
...
很久之前做的,代码也还看得过去吧。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ar, br, ac, bc; //行列
int a[100][100], b[100][100], c[100][100]; //矩阵
int i, j, k, n; //*变量
int ai = 0, ai_bj = 0, bi = 0, ci = 0, cj = 0, t, p; //
int temp = 0; //临时变量
//input
scanf("%d %d", &ar, &ac);
for (i = 0; i < ar; i++)
{
for (j = 0; j < ac; j++)
{
scanf("%d", &a[i][j]);
}
}
scanf("%d %d", &br, &bc);
for (i = 0; i < br; i++)
{
for (j = 0; j < bc; j++)
{
scanf("%d", &b[j][i]);
}
}
//judge
if (ac != br)
{
printf("Error: %d != %d", ac, br);
}
else
{
//mutiple
t = 0;
p = 0;
while (ai < ar)
{
while (bi < bc)
{
for (i = 0; i < ac; i++)
{
temp += a[ai][i] * b[bi][i];
}
c[t][p++] = temp;
temp = 0;
bi++;
}
ai++;
t++;
p = 0;
bi = 0;
}
printf("%d %d\n", ar, bc);
for (i = 0; i < ar; i++)
{
for (j = 0; j < bc; j++)
{
printf("%d", c[i][j]);
if (j != bc - 1)
printf(" ");
}
printf("\n");
}
}
// system("pause");
return 0;
}