zoukankan      html  css  js  c++  java
  • 矩阵运算(二维数组)

     1 package cn.bjsxt.array2;
     2 /**
     3  * 
     4  * 矩阵加法
     5  * 乘法
     6  * @author Administrator
     7  *
     8  */
     9 public class Matrix {
    10     public static void print(int[][] c){
    11         //打印矩阵
    12         for(int i=0;i<c.length;i++){
    13             for(int j=0;j<c.length;j++){
    14                 System.out.print(c[i][j]+"	");
    15             }
    16             System.out.println();
    17         }
    18     }
    19     public static int[][] add(int[][]a,int[][]b){
    20         //加法
    21         int[][]c=new int[a.length][a.length];
    22         for(int i=0;i<c.length;i++){
    23             for(int j=0;j<c.length;j++){
    24                 c[i][j]=a[i][j]+b[i][j];
    25             }
    26         }
    27         return c;
    28     }
    29     //矩阵乘法
    30     public static int[][] multiply(int[][]a,int[][]b){
    31         //加法
    32         int[][]c=new int[a.length][a.length];
    33         for(int i=0;i<c.length;i++){
    34             for(int j=0;j<c.length;j++){
    35                 c[i][j]=a[i][j]*b[i][j];
    36             }
    37         }
    38         return c;
    39     }
    40     
    41     public static void main(String[] args) {
    42         
    43     
    44     int[][] a ={ 
    45                     {1,3,3},
    46                     {2,4,7},
    47                     {6,4,9}
    48                 };
    49     int[][]    b = {
    50                     {3,3,3},
    51                     {2,4,7},
    52                     {1,5,7}
    53                 };
    54     
    55     
    56         int[][]    c = add(a,b);
    57     
    58         print(c);
    59         System.out.println("#############################");
    60         
    61         int[][]d = multiply(a,b);
    62         print(d);
    63 
    64 
    65     }
    66 }
  • 相关阅读:
    Eclipse RCP与Spring OSGi:技术详解与最佳实践
    AutoCAD 2016机械设计从入门到精通(第2版)
    中文版CorelDRAW X7平面设计
    神奇的中文版Photoshop CC 2017入门书
    Hadoop实战(第2版)
    1048.判断三角形类型
    1046.求最大值
    1047.素数判定
    1056.最大公约数
    1057.众数
  • 原文地址:https://www.cnblogs.com/PoeticalJustice/p/7625922.html
Copyright © 2011-2022 走看看