Problem description given an n-order matrix A, the M power of output A (m non-negative integer)
For example:
A =
1 2
3 4
A power of 2 times
7 10
15 22 input Format the first line is a positive integer N, M (1<=n<=30, 0<=m<=5), representing the order of the matrix A and the required power number
Next n rows, each row n absolute value does not exceed 10 non-negative integer, the output format of the description matrix A outputs a total of n rows, n integers per row, representing the matrix corresponding to the M power of a. Separate sample input with a space between adjacent numbers 2 2
1 2
3 4 Sample Output 7 10
15 22The code is as follows:
#include <stdio.h>
/* Output matrix function */
void print (int c[][101],int n) {
for (int i=0;i<n;i++) {
for ( int j=0;j<n;j++) {
printf ("%d", c[i][j]);
}
printf ("\ n");
}
}
/* Matrix multiplication function */
void chengfa (int a[][101],int b[][101],int c[][101],int n,int m) {
for (int p=1;p<m;p + +) {
for (int. i=0;i<n;i++) {
for (int j=0;j<n;j++) {
int t=0;
for (int k=0;k<n;k++) {
T + = a[i][k]*b[k][j];
C[i][j]=t;
}
}
}
for (int. i=0;i<n;i++) {
for (int j=0;j<n;j++) {
B[i][j]=c[i][j];
}
}
}
}
int main () {
int n,m;
int a[101][101]={0},b[101][101]={0},c[101][101]={0};
scanf ("%d%d", &n,&m);
/* Input Matrix */
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
scanf ("%d", &a[i][j]);
}
}
/* Record Matrix */
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
B[I][J]=A[I][J];
}
}
if (m==0) {
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (I==J)
C[i][j]=1;
}
}
Print (c,n);
}
else if (m==1) {
Print (a,n);
}
else{
CHENGFA (A,B,C,N,M);
Print (c,n);
}
return 0;
}
C language · Matrix multiplication · Algorithm Training