先從main函數(shù)開始運行,輸入一個n的值,接下來調(diào)用zhuanhuan函數(shù),并把n作為參數(shù)傳給zhuanhuan函數(shù),n也就等于m,接著整個程序便轉(zhuǎn)到了zhuanhuan函數(shù)那里,這個函數(shù)中定義了一個長整型變量x,由這個變量的值來決定是否繼續(xù)下面的調(diào)用,把m除10的值賦值給x,當x不等于0時就繼續(xù)下面的語句實現(xiàn)調(diào)用,只要x值不為0,就一直循環(huán)這個過程
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網(wǎng)站設計制作、成都網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的魏都網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!
/*用c語言中的函數(shù)遞歸調(diào)用算法實現(xiàn)n階矩陣的n次冪*/
#include stdio.h
#include stdlib.h
#include time.h
#include string.h
//創(chuàng)建矩陣,矩陣用一維數(shù)組存儲
double *matCreate(unsigned int m, unsigned int n)
{
double *p = (double *)malloc(sizeof(double) * m * n);
if (p == NULL) printf("創(chuàng)建矩陣失??!\n");
return p;
}
//輸入矩陣元素
void matInput(double *a, unsigned int m, unsigned int n)
{
for (int i = 0; i m; ++i)
{
for (int j = 0; j n; ++j)
{
scanf("%f ", a[i * n + j]);
}
}
return;
}
//隨機產(chǎn)生矩陣元素,均勻分布于[from to]
void matInitRand(double *a, unsigned int m, unsigned int n, double from, double to)
{
if (a == NULL || m = 0 || n = 0) return;
double x;
srand(time(NULL));
for (int i = 0; i m; ++i)
{
for (int j = 0; j n; ++j)
{
x = (1.0 * rand() / RAND_MAX) * (to - from) + from;
a[i * n + j] = x;
}
}
return;
}
//轉(zhuǎn)置
void matTranspose(double *a, double *b, unsigned int m, unsigned int n)
{
for (int i = 0; i m; ++i)
{
for (int j = 0; j n; ++j)
{
b[j*n +i]=a[i * n + j] ;
}
}
}
//輸出矩陣
void matPrint(double *a, unsigned int m, unsigned int n)
{
for (int i = 0; i m; ++i)
{
for (int j = 0; j n; ++j)
{
printf("%8.4f ", a[i * n + j]);
}
putchar('\n');
}
return;
}
//矩陣乘法c=a*b
void matMul(double *a, double *b, double *c, unsigned int m, unsigned int n, unsigned int k)
{
if (a == NULL || b == NULL || c == NULL || m = 0 || n = 0 || k = 0) return;
double x = 0.0f;
for (int i = 0; i m; ++i)
{
for (int u = 0; u k; ++u)
{
x = 0.0f;
for (int j = 0; j n; ++j)
{
x += a[i * n + j] * b[j * k + u];
}
c[i * k + u] = x;
}
}
return;
}
//b=a^n, a:m*m階矩陣
void matFac(double *a, double *b, unsigned int n, unsigned int m)
{
double *c = (double *)malloc(sizeof(double) * m * m); //保存臨時結(jié)果
if (n 1)
{
matFac(a, c, n - 1, m);
matMul(a, c, b, m, m, m);
}
else
memcpy(b, a, sizeof(double)*m * m);
// printf("%d:\n",n);
// matPrint(b, m,m);
free(c); //回收內(nèi)存
return ;
}
#define M 3
#define N 4
#define K N
int main(int argc, char const *argv[])
{
double *A, *B, *B1,*BT, *C;
A = matCreate(M, N);
B = matCreate(N, K);
B1 = matCreate(N, K);
BT = matCreate(K,N);
C = matCreate(M, K);
if (!A || !B || !B1 || !BT || !C) return -1;
matInitRand(A, M, N, 0.0f, 1.0f);
printf("A=\n");
matPrint(A, M, N);
matInitRand(B, N, K, 0.0f, 1.0f);
printf("B=\n");
matPrint(B, N, K);
matTranspose(B,BT,N,K);
printf("B'=\n");
matPrint(BT, K,N);
matMul(A, B, C, M, N, K);
printf("C=A*B\n");
matPrint(C, M, N);
matFac(B, B1, 4, N);
printf("B^4\n");
matPrint(B1, N, K);
return 0;
}
#include stdio.h
int main()
{ struct aaa
{ int x;
int y;
};
struct aaa t,a[3][3]= {0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1,2,2};
int i,j;
for(i=0; i3; i++)
{ for(j=0; j3; j++)
printf("[%d,%d]? ",a[i][j].x,a[i][j].y);
printf("\n");
}
printf("after:\n");
for(i=0; i3; i++)
for(j=0; ji; j++)
{ t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;
}
for(i=0; i3; i++)
{ for(j=0; j3; j++)
printf("[%d,%d]? ",a[i][j].x,a[i][j].y);
printf("\n");
}
return 0;
}
或:
#include stdio.h
struct aaa
{ int x;
int y;
};
void zhuanzhi(struct aaa a[][3])
{ struct aaa t;
int i,j;
for(i=0; i3; i++)
for(j=0; ji; j++)
{ t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;
}
}
void prt(struct aaa a[][3])
{ int i,j;
for(i=0; i3; i++)
{ for(j=0; j3; j++)
printf("[%d,%d]? ",a[i][j].x,a[i][j].y);
printf("\n");
}
}
int main()
{ struct aaa a[3][3]= {0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1,2,2};
prt(a);
zhuanzhi(a);
printf("after:\n");
prt(a);
return 0;
}