------------------------------------------------------------
The first time to feel the importance of good study Qaq
Before doing this problem, please learn: Matrix multiplication (Baidu Encyclopedia)
The power of the matrix is 0: Diagonal is 1 Other values are 0 for example
Results
------------------------------------------------------------
Algorithm
1 ImportJava.util.*;2 Public classMain {3 Public Static voidMain (string[] args) {4Scanner sc =NewScanner (system.in);5 intn =sc.nextint ();6 intm =sc.nextint ();7 Long[] A =New Long[n][n];8 Long[] B =New Long[n][n];9 inti,j;Ten for(i=0;i<n;i++) One for(j=0;j<n;j++) AB[I][J]=A[I][J] =Sc.nextlong (); - if(m==0) - for(i=0;i<n;i++){ the for(j=0;j<n;j++){ - if(I==J) System.out.print (1+ ""); - ElseSystem.out.print (0+ ""); - } + System.out.println (); - } + Else if(m==1) A for(i=0;i<n;i++){ at for(j=0;j<n;j++) -System.out.print (a[i][j]+ ""); - System.out.println (); - } - Else{ - for(intz=1;z<m;z++){ in Long[] tmp =New Long[n][n]; - for(i=0;i<n;i++){ to for(j=0;j<n;j++){ + LongAdd = 0; - for(inty=0;y<n;y++){ theAdd + = a[i][y]*B[y][j]; * } $TMP[I][J] =add;Panax Notoginseng } - } theb =tmp; + } A for(i=0;i<n;i++){ the for(j=0;j<n;j++) +System.out.print (b[i][j]+ ""); - System.out.println (); $ } $ } - - } the}
Java Basic Practice matrix multiplication for Blue Bridge net questions