zoukankan      html  css  js  c++  java
  • 华为机试测试-矩阵乘法-循环

     1 import java.util.Scanner;
     2 public class Main{
     3       public static void main(String[] args){
     4           Scanner scanner=new Scanner(System.in);
     5           int row=scanner.nextInt();
     6           int len=scanner.nextInt();
     7           int column=scanner.nextInt();
     8           int [][] x1=new int[row][len];
     9           int [][] x2=new int[len][column];
    10           for(int i=0;i<row;i++)
    11           {
    12               for(int j=0;j<len;j++)
    13               {
    14                   x1[i][j]=scanner.nextInt();
    15               }
    16           }
    17           for(int i=0;i<len;i++)
    18           {
    19               for(int j=0;j<column;j++)
    20               {
    21                   x2[i][j]=scanner.nextInt();
    22               }
    23           }
    24           int [][]d=matrixMultity(x1, x2);
    25           for(int i=0;i<row;i++)
    26           {
    27               for(int j=0;j<column;j++)
    28               {
    29                   if(j==column-1)
    30                       System.out.print(d[i][j]);
    31                   else
    32                       System.out.print(d[i][j]+" ");
    33               }
    34               System.out.println();
    35           }
    36           scanner.close();
    37       } 
    38       
    39       /**
    40        * 
    41        * @param x1
    42        * @param x2
    43        * @return
    44        */
    45       public static int [][] matrixMultity(int [][] x1,int[][] x2)
    46       {
    47           int row=x1.length,column=x2[0].length;
    48           int len=x1[0].length;
    49           int [][] d=new int[row][column];
    50           for(int i=0;i<row;i++)
    51           {
    52               for(int j=0;j<column;j++)
    53               {
    54                   int sum=0;
    55                   for(int k=0;k<len;k++)
    56                   {
    57                       sum+=x1[i][k]*x2[k][j];
    58                   }
    59                   d[i][j]=sum;
    60               }
    61           }
    62           return d;
    63       }
    64 }
  • 相关阅读:
    自然数幂和的若干种解法
    线性预处理逆元
    差分与有限微积分
    UVALive 6859——凸包&&周长
    UVALive 6858——分类讨论&&水题
    UVALive 6862——结论题&&水题
    ZOJ4019——贪心&&DP
    [LeetCode] Power of Two
    循环队列实现(C++) Ring Buffer
    正确使用stl vecotr erase函数
  • 原文地址:https://www.cnblogs.com/maydow/p/4782196.html
Copyright © 2011-2022 走看看