十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
double power_negative(double n,int p)
成都做网站、网站建设介绍好的网站是理念、设计和技术的结合。创新互联拥有的网站设计理念、多方位的设计风格、经验丰富的设计团队。提供PC端+手机端网站建设,用营销思维进行网站设计、采用先进技术开源代码、注重用户体验与SEO基础,将技术与创意整合到网站之中,以契合客户的方式做到创意性的视觉化效果。
{
double pow = 1;
int q;
q=-p;
if(q0)
pow = power_negative(n,1-q) / n;
return pow;
}
改成这样,虽然你那个写的是递归调用,但是返回的却是1/pow,那么就会是0.5 * 2 * 0.5 * 2 * 0.5这样的形式返回,所以最终无论是多少,结果都是0.5,而且递归时应该用1-q,因为你调用负数求幂,必须使参数为负才会正确
/*用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;
}
#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,这是规定,也是合理的)
#include
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;
}
#includestdio.h
int
power(int
x,int
n)
{
if(n
==
0)
//任何数的0次方都是1
return
0;
else
if(n
==1)
//如果是1次方
则返回本来的值
return
x;
else
//否则递归循环
return
x*power(x,n-1);
}
main()
{
printf("%d
",power(3,3));
printf("%d
",power(4,2));
getchar();
return
0;
}
long
double
_pow_i(
long
double
_X,
int
_Y
)
{
if
(
!_Y
)
return
1;
//
次幂为0的情况
if
(
!(_Y-1)
)
return
_X;
//
当_Y
=
1的情况则返回结果_X
return
_X
*
_pow_i(
_X,
abs(_Y)-1
);
//
每一步返回
_X
*
上一次的乘积,_Y
减1计数
}
long
double
_pow(
long
double
_X,
int
_Y
)
{
long
double
_Z
=
_pow_i(
_X,
_Y
);
return
_Y
?
1
/
_Z
:
_Z;
}
因为写在一起的话不好解释,所以分开正负的情况,_pow就是判断次幂是否为负数,是负数就等于1/那个数个正次幂。