网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

c语言递归函数矩阵转换,c语言阶乘函数 递归

转换递归函数 c语言

先从main函数开始运行,输入一个n的值,接下来调用zhuanhuan函数,并把n作为参数传给zhuanhuan函数,n也就等于m,接着整个程序便转到了zhuanhuan函数那里,这个函数中定义了一个长整型变量x,由这个变量的值来决定是否继续下面的调用,把m除10的值赋值给x,当x不等于0时就继续下面的语句实现调用,只要x值不为0,就一直循环这个过程

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计制作、成都网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的魏都网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

如何用c语言中的函数递归调用算法实现n阶矩阵的n次幂的求解?

/*用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;

}

c语言如何通过定义函数来转置矩阵?

#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;

}


网站题目:c语言递归函数矩阵转换,c语言阶乘函数 递归
文章起源:http://bjjierui.cn/article/dsgcooo.html

其他资讯