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 }

     

     

     

     

     

     

  • 相关阅读:
    js this
    python词云的制作方法
    flask表单标签
    scrapy使用PhantomJS爬取数据
    flask连接sqlalchemy数据库,实现简单的登录跳转功能
    useful tools and website
    sqlalchemy精华版
    flask连接数据库mysql+SQLAlchemy
    flask框架get post方式
    flask基础知识
  • 原文地址:https://www.cnblogs.com/loveluking/p/6087177.html
Copyright © 2011-2022 走看看