[求助]怎么用递归调用编x的n次方
请教大家:怎么用递归调用来编写x的n次方?谢谢! 搜索更多相关的解决方案:
递归
----------------解决方案--------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
void square(int x, int n, int *result);
int main(void)
{
int x, n, result = 1;
printf("Enter x and n: \n");
scanf("%d %d", &x, &n);
square(x, n, &result);
printf("result = %d\n", result);
exit(0);
}
void square(int x, int n, int *result)
{
if (n > 0) {
(*result) *= x;
square(x, --n, &(*result));
}
}
----------------解决方案--------------------------------------------------------
//这个递归函数计算x的n次方
void f(x, n) {
if (n == 1) return x;
return f(x,n-1)*x;
}
----------------解决方案--------------------------------------------------------
谢谢两位.
----------------解决方案--------------------------------------------------------