问题描述
给定一个N阶矩阵A,输出A的M次幂(M是非负整数)
例如:
A =
1 2
3 4
A的2次幂
7 10
15 22
输入格式
第一行是一个正整数N、M(1<=N<=30, 0<=M<=5),表示矩阵A的阶数和要求的幂数
接下来N行,每行N个绝对值不超过10的非负整数,描述矩阵A的值
输出格式
输出共N行,每行N个整数,表示A的M次幂所对应的矩阵。相邻的数之间用一个空格隔开
样例输入
2 2
1 2
3 4
样例输出
7 10
15 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package base17; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); in.close(); long[][] a = new long[40][40]; long[][] b = new long[40][40]; long[][] t = new long[40][40]; //输入 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = in.nextLong(); t[i][j] = a[i][j]; } } //0次幂 输出单位矩阵 if (m == 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { System.out.print(1 + " "); } else { System.out.print(0 + " "); } } System.out.println(); } return; } //m次幂 while (--m > 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int k = n; while (k > 0) { b[i][j] += t[i][k - 1] * a[k - 1][j]; k--; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { t[i][j] = b[i][j]; b[i][j] = 0; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(t[i][j] + " "); } System.out.println(); } } } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼