c语言递归函数的n次幂 c++用递归方法求n!

C语言 利用递归函数求X的n次方程序填空#include "stdio.h"
#include "math.h"
main()
{
int a, b;
long power(), t;
printf("输入两个整数c语言递归函数的n次幂:");
scanf("%d, %d", a, b);
t = power(a, b);
printf("%d ^ %d = %1d", a, b, t);
}
long power(x ,n)
int x, n;
{
long y;
if(n0)
y = x * power(x, n-1);
else y = 1;
return y;
}
请教一道c语言题,利用递归函数计算x的n次幂 。#includestdio.h
int Xn(int x,int n)//xc语言递归函数的n次幂的n次幂(n=0)
【c语言递归函数的n次幂 c++用递归方法求n!】{
if(n=0)return 1;
else if(n=1) return x;
else return(Xn(x,n-1));
}
void main()
{
int a,b,xn;
scanf("%d %d",a,b);
xn=Xn(a,b);//ac语言递归函数的n次幂的b次幂
printf("The result is:%d",xn);
}
如何用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; im; ++i)
{
for (int j = 0; jn; ++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; im; ++i)
{
for (int j = 0; jn; ++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; im; ++i)
{
for (int j = 0; jn; ++j)
{
b[j*n +i]=a[i * n + j] ;
}
}
}
//输出矩阵
void matPrint(double *a, unsigned int m, unsigned int n)
{
for (int i = 0; im; ++i)
{
for (int j = 0; jn; ++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, unsignedint 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; im; ++i)
{
for (int u = 0; uk; ++u)
{
x = 0.0f;
for (int j = 0; jn; ++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 (n1)
{
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);

推荐阅读