递归实现n的k次方
代码示例:
#include <stdio.h>double Pow(int n, int k)
{if (k > 0){return n * Pow(n, k - 1);}else if (0 == k){return 1;}else{return 1.0 / pow(n, -k);}
}
int main()
{int n = 0;int k = 0;scanf("%d %d", &n, &k);double ret = Pow(n, k);printf("%lf", ret);return 0;
}
运行结果:
2 3
8.000000
2 0
1.000000
2 -3
0.125000