zoukankan      html  css  js  c++  java
  • 矩阵相乘问题

     1 矩阵乘法(二维数组)
     2 
     3     Time Limit: 1800/600 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
     4     Total Submission(s): 633     Accepted Submission(s): 175
     5 
     6 Description
     7 
     8 天才少女Alice,哦,不,应该叫天才幼儿才对,她才1岁7个月,就开始学习线性代数了。她很快就掌握了矩阵加法、减法,马上又学乘法。
     9 
    10 矩阵乘法是这样定义的:
    11 一个m行n列的矩阵A 与 一个n行p列的矩阵B 的乘积,是一个m行p列的矩阵C。
    12 
    13 C的第i行第j个元素是这样算出来的:
    14 C(i,j) = A(i,1)*B(1,j) + A(i,2)*B(2,j) + ... + A(i,k)*B(k,j) + ... + A(i,n)*B(n,j)
    15 即,C的第i行第j个元素,是由A的第i行与B的第j列的元素相应做乘法,最后求和所得。
    16 
    17 矩阵A与矩阵B可以做乘法的前提是:A的列数等于B的行数。
    18 
    19 对于m、n、p比较小的情况,Alice很快就能计算完毕。算出来的结果如果没有错,那么就会嚷嚷着要奖励吃蛋糕。并且常常以做矩阵乘法为理由,向爸爸索要蛋糕吃。坏坏的爸爸于是出了一些矩阵让Alice做,但m、n、p的值可能很大( 1 ≤ m、n、p ≤ 300 ),那么Alice就没有那么快计算完了。
    20 
    21 Alice一看那么大的矩阵,立刻傻眼了,心想:算一个礼拜也算不完啊。。。。于是,狡猾的Alice又用水汪汪的大眼睛向你发出哀求的目光。请大哥哥/大姐姐快点编个程序帮Alice算出来吧。
    22 Input
    23 
    24 输入的第一行是三个数m、n、p ( 1 ≤ m、n、p ≤ 300 ),接下来的m行每行是n个空格分隔的整数,组成一个m*n的矩阵A。
    25 接下来是n行,每行是p个空格分隔的整数,组成一个n*p的矩阵B。
    26 Output
    27 
    28 输出 A*B的积矩阵C  :m行,每行p个整数(空格分隔)。C的元素不超unsigned long的表示范围。
    29 Sample Input
    30 
    31 3 3 2
    32 7 5 0
    33 0 2 1
    34 1 3 0
    35 1 1
    36 0 1
    37 19 23
    38 Sample Output
    39 
    40 7 12
    41 19 25
    42 1 4
    43 Author
    44 John

    代码:

      1 //import java.util.Arrays;
      2 import java.util.Scanner;
      3 public class oj1099 
      4 {
      5     public static void main(String args[])
      6     {
      7         
      8         Scanner in=new Scanner(System.in);
      9         
     10         int m=in.nextInt();
     11         int n=in.nextInt();
     12         int p=in.nextInt();
     13         int a[][] =new int[m][n];
     14         int b[][] =new int[n][p];
     15         
     16         
     17         for(int i=0;i<m;i++)//矩阵a
     18             for(int j=0;j<n;j++)
     19             {
     20                 a[i][j]=in.nextInt();
     21                 /*if(j%n==0)
     22                 {
     23                     System.out.print("
    ");
     24                 }*/
     25                 
     26             }
     27         for(int u=0;u<n;u++)//矩阵b
     28             for(int v=0;v<p;v++)
     29             {
     30                 b[u][v]=in.nextInt();
     31                 /*if(v%p==0)
     32                 {
     33                     System.out.print("
    ");
     34                 }*/
     35             }
     36         /*int c[][]=new int[n][p];
     37          * for(int e=0;e<n;e++)//求矩阵c 自己想的
     38             for(int f=0;f<p;f++)    
     39             {
     40                 int result=0;
     41                 for(int x=0;x<n;x++)
     42                 {
     43                     for(int y=0;y<n;y++)
     44                     {
     45                         c[e][f]=result+a[e][x]*b[x][f];                        
     46                     }
     47                 }
     48         
     49             }*/
     50         int r[][]=new int[n][p];
     51         for(int e=0;e<n;e++)//求矩阵c 自己想的改错方法
     52             for(int f=0;f<p;f++)    
     53             {
     54                  r[e][f]=0;
     55                 for(int x=0;x<n;x++)
     56                 {
     57                     
     58                     r[e][f]=r[e][f]+a[e][x]*b[x][f];        
     59                 }
     60                 
     61             }
     62         
     63         
     64         /*int r[][]=new int[n][p];//自己根据网上想出来的!
     65           for(int i=0;i<n;++i)
     66           {
     67               for(int j=0;j<p;++j)
     68               {
     69                //每一个r[i][j]的运算:
     70                r[i][j]=0;//初始化
     71                for(int k=0;k<m;++k)
     72                    r[i][j]+=a[i][k]*b[k][j];
     73                }
     74           }*/
     75           /*int[][] r = new int[a[0].length][b[0].length];//网上做法
     76           for(int i=0;i<r.length;++i)
     77           {
     78               for(int j=0;j<r[i].length;++j)
     79               {
     80                //每一个r[i][j]的运算:
     81                r[i][j]=0;//初始化
     82                for(int k=0;k<b.length;++k)
     83                    r[i][j]+=a[i][k]*b[k][j];
     84                }
     85           }*/
     86           //输出结果
     87           /*for(int i=0;i<r.length;++i)
     88            System.out.println(Arrays.toString(r[i]));*/
     89           
     90           
     91         
     92         for(int g=0;g<n;g++)//输出
     93             for(int h=0;h<p;h++)
     94             {
     95                 System.out.print(r[g][h]+" ");
     96                 if((h+1)%p==0)
     97                 {
     98                     System.out.print("
    ");
     99                 }
    100             }
    101         
    102         
    103     }
    104 
    105 }

    这么一道题目。对自己熟悉java代码有一定作用。这是学校OJ网1099题目。

    ---- 动动手指关注我!或许下次你又能在我这里找到你需要的答案!ZZZZW与你一起学习,一起进步!
  • 相关阅读:
    使用Myeclipse + SVN + TaoCode 免费实现项目版本控制的详细教程
    国内的代码托管服务
    国内可用的SVN和Git代码托管网站汇总
    需求调研与分析流程
    如何做好新项目的需求调研?(一)
    如何进行有效的需求调研
    weblogic和tomcat
    同步变量也是变量
    并发编程的三个管理
    机器学习
  • 原文地址:https://www.cnblogs.com/zzzzw/p/4524184.html
Copyright © 2011-2022 走看看