求m階矩陣的n次冪


題目如題

直接上代碼了。

 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 }

 

不解釋。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM