double power(double x, int n)
{
if (n < 0)
{
printf("n<0,data error!\n");
return 0;
}
if (n == 1 || x == 0)
return 1;
return x * power(x, n - 1);
}
思路:题目要求 求 x的n次方
即 x * x *..
调用递归来求,也就是说,当n 不为零时一直调用下去
则伪代码 为
if n == 0
then return 1.0
else
return x * power(x,n - 1)