zoukankan      html  css  js  c++  java
  • 蓝桥网试题 java 基础练习 矩阵乘法

    ------------------------------------------------------------

    第一次感觉到好好学习的重要性QAQ

    在做这道题之前请先学会 :矩阵乘法(百度百科)

    矩阵的0次幂:对角线为1 其他值为0 例如

    结果

    ------------------------------------------------------------

     算法

     1 import java.util.*;
     2 public class Main {
     3     public static void main(String[] args) {
     4         Scanner sc = new Scanner(System.in);
     5         int n = sc.nextInt();
     6         int m = sc.nextInt();
     7         long[][] a = new long[n][n];
     8         long[][] b = new long[n][n];
     9         int i,j;
    10         for(i=0;i<n;i++)
    11             for(j=0;j<n;j++)
    12                 b[i][j]=a[i][j] = sc.nextLong();
    13         if(m==0)
    14             for(i=0;i<n;i++){
    15                 for(j=0;j<n;j++){
    16                     if(i==j)System.out.print(1+" ");
    17                     else System.out.print(0+" ");
    18                 }
    19                 System.out.println();
    20             }
    21         else if(m==1)
    22             for(i=0;i<n;i++){
    23                 for(j=0;j<n;j++)
    24                     System.out.print(a[i][j]+" ");
    25                 System.out.println();
    26             }
    27         else{
    28             for(int z=1;z<m;z++){
    29                 long[][] tmp = new long[n][n]; 
    30                 for(i=0;i<n;i++){
    31                     for(j=0;j<n;j++){
    32                         long add = 0;
    33                         for(int y=0;y<n;y++){
    34                             add += a[i][y]*b[y][j];
    35                         }
    36                         tmp[i][j] = add;
    37                     }
    38                 }
    39                 b = tmp;
    40             }
    41             for(i=0;i<n;i++){
    42                 for(j=0;j<n;j++)
    43                     System.out.print(b[i][j]+" ");
    44                 System.out.println();
    45             }
    46         }
    47             
    48     }
    49 }

     

     

     

     

     

     

  • 相关阅读:
    数据提交方式:post和get
    什么是SQL注入式攻击和如何防范?
    CSS3 @keyframes 规则
    php数据库连接及简单操作
    深入理解CSS过渡transition
    css 选择器
    利用border制作三角形原理
    iOS 8 自适应 Cell
    dSYM 文件分析工具
    iOS开发使用半透明模糊效果方法整理
  • 原文地址:https://www.cnblogs.com/loveluking/p/6087177.html
Copyright © 2011-2022 走看看