扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
#include "stdio.h"
创新互联长期为上千多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为洋县企业提供专业的成都网站设计、网站制作,洋县网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。
#include "math.h"
main()
{
int a, b;
long power(), t;
printf("输入两个整数:");
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;
}
#includestdio.h
int power(int x,int n)
{
if(n==0)
return 1;
elseif(n%2==1)
return x*power(x,n-1);
else{
int y=power(x,n/2);
return y*y;
}
}
int main()
{
int a,b,c;
printf("enter x and n:");
setvbuf(stdout,NULL,_IONBF,0);
scanf("%d%d",a,b);
c=power(a,b);
printf("结果为%d",c);
return 0;
}
扩展资料
#includestdio.h
double power(double x,int n);
main()
{
double x;
int n;
printf("Input x,n:");
scanf("%lf,%d",x,n);
printf("%.2lf",power(x,n));
}
double power(double x,int n)
{
double a=1.0;
int i;
for(i=1;i=n;i++)
a*=x;
return a;
}
参考资料:百度百科 - 递归调用
首先纠正一下,C语言不能重载同名函数,C++也不能仅以返回值的区别来重载。所以可以统一使用double recursive_pow_pos(int n, int m)。
我帮你做了个示范,应该很容易看懂。
(注意,0^0=1,这是规定,也是合理的)
#includestdio.h
double recursive_pow_pos(int n, int m)
{
register double t;
if (m == 0)
return 1.0;
if (m 0)
return 1.0 / recursive_pow_pos(n, -m);
t = recursive_pow_pos(n, m / 2);
if (m % 2)
return t * t * (double)n;
else
return t * t;
}
int main(void)
{
int n, m;
double result;
double recursive_pow_pos(int, int);
printf("Input n-");
scanf("%d", n);
printf("Input m-");
scanf("%d", m);
result = recursive_pow_pos(n, m);
printf("%d ^ %d = %f\n", n, m, result);
return 0;
}
#include
stdio.h
//
递归函数计算x的n次幂,这里都是用的int,因此计算范围只能限制在int类型的取值范围下
//
如果需要计算更大的值,需要将类型修改为float
int
mypow(int
x,
int
n)
{
if
(n
=
0)
return
1;
else
return
x
*
mypow(x,
n
-
1);
}
int
main()
{
printf("2^32
=
%d\n",
mypow(2,
10));
//
计算2的10次方
return
0;
}
#includestdio.h
int Xn(int x,int n)//x的n次幂(n=0)
{
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);//a的b次幂
printf("The result is:%d",xn);
}
/*用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;
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流