藍橋網試題 java 基礎練習 矩陣乘法

來源:互聯網
上載者:User

標籤:學習   style   color   百度   new   imp   else   jmx   image   

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

第一次感覺到好好學習的重要性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 }

 

 

 

 

 

 

 

藍橋網試題 java 基礎練習 矩陣乘法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.