十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
先从main函数开始运行,输入一个n的值,接下来调用zhuanhuan函数,并把n作为参数传给zhuanhuan函数,n也就等于m,接着整个程序便转到了zhuanhuan函数那里,这个函数中定义了一个长整型变量x,由这个变量的值来决定是否继续下面的调用,把m除10的值赋值给x,当x不等于0时就继续下面的语句实现调用,只要x值不为0,就一直循环这个过程
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计制作、成都网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的魏都网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
/*用c语言中的函数递归调用算法实现n阶矩阵的n次幂*/
#include stdio.h
#include stdlib.h
#include time.h
#include string.h
//创建矩阵,矩阵用一维数组存储
double *matCreate(unsigned int m, unsigned int n)
{
double *p = (double *)malloc(sizeof(double) * m * n);
if (p == NULL) printf("创建矩阵失败!\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;
}
//随机产生矩阵元素,均匀分布于[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;
}
//转置
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); //保存临时结果
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); //回收内存
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;
}