題目如題
直接上代碼了。
1 import java.util.Scanner; 2 3 public class test 4 { 5 public static void main(String [] args) 6 { 7 int m,n,i,j,p; 8 int array[][]=new int[100][100]; 9 int temp[][]=new int[100][100]; 10 Scanner scanner=new Scanner(System.in); 11 System.out.printf("input m,n:"); //m表示矩陣的階數,n表示的冪數 12 m=scanner.nextInt(); 13 n=scanner.nextInt(); 14 System.out.printf("input the value of matrix \n"); 15 for(i=0;i<m;i++) //輸入矩陣 16 { 17 for(j=0;j<m;j++) 18 { 19 array[i][j]=scanner.nextInt(); 20 } 21 } 22 for(i=0;i<m;i++) 23 { 24 for(j=0;j<m;j++) 25 { 26 for(p=0;p<m;p++) 27 temp[i][j]+=array[i][p]*array[p][j]; 28 } 29 } 30 31 System.out.printf("the mth power of matrix:\n"); 32 for(i=0;i<m;i++) //輸出結果 33 { 34 for(j=0;j<m;j++) 35 { 36 System.out.printf("%3d",temp[i][j]); 37 } 38 System.out.printf("\n"); 39 } 40 } 41 }
不解釋。