zoukankan      html  css  js  c++  java
  • c语言中求两个矩阵的乘积

    1、

    #include <stdio.h>
    
    int main(void)
    {
        int a[4][3] = {{2,5,4},{1,2,4},{5,3,2},{4,2,6}};
        int b[3][4] = {{2,1,6,2},{3,2,4,1},{5,2,1,2}};
        int i, j, k;
        int c[4][4] = {0};
        
        for (i = 0; i < 4; i++)
        {
            for (k = 0; k < 4; k++)
            {
                for (j = 0; j < 3; j++)
                {
                    c[i][k] += a[i][j] * b[j][k];
                }
            }
        }
        
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 4; j++)
            {
                printf("%d ", c[i][j]);
            }
            putchar('\n');
        }
        return 0;
    } 

     

    #include <stdio.h>
    
    int main(void)
    {
        int a[4][3], b[3][4];
        int i, j;
        puts("please input the elements of 4 * 3 matrix.");
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 3; j++)
            {
                printf("a[%d][%d] =  ", i, j); scanf("%d", &a[i][j]);
             } 
        }
        
        puts("please input the elements of 3 * 4 matrix.");
        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 4; j++)
            {
                printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]);
            }
        }
        
        
        int k;
        int c[4][4] = {0};
        for (i = 0; i < 4; i++)
        {
            for (k = 0; k < 4; k++)
            {
                for (j = 0; j < 3; j++)
                {
                    c[i][k] += a[i][j] * b[j][k];
                }
            }
        }
        puts("output the multiply of the two matrix.");
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 4; j++)
            {
                printf("%d ", c[i][j]);
            }
            putchar('\n');
        }
        return 0;
    }

    ↓ 

    #include <stdio.h>
    
    int main(void)
    {
        int a[2][4], b[4][5];
        int i, j;
        puts("please input the elements of the 2 * 4 matrix");
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 4; j++)
            {
                printf("a[%d][%d] = ", i, j); scanf("%d", &a[i][j]);
            }
        }
        puts("please input the elements of the 4 * 5 matrix");
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 5; j++)
            {
                printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]);
            }
        }
        int c[2][5] = {0};
        int k;
        for (i = 0; i < 2; i++)
        {
            for (k = 0; k < 5; k++)
            {
                for (j = 0; j < 4; j++)
                {
                    c[i][k] += a[i][j] * b[j][k];
                }
            }
        }
        puts("output the multiply of the two matrix.");
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 5; j++)
            {
                printf("%d ", c[i][j]);
            }
            putchar('\n');
        }
        return 0;
    }

  • 相关阅读:
    插入迭代器
    operator[] 和 insert
    STL注意比较函数
    remove、erase
    reserve的使用
    allocator
    【转】【模板】求割点和桥
    【模拟,时针分针秒针两两夹角】【没有跳坑好兴奋】hdu
    【强连通分量分解】
    【模拟ACM排名】ZOJ-2593 Ranking (Andrew Stankevich’s Contest #5)
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14380289.html
Copyright © 2011-2022 走看看