递归的方法
#include<stdio.h> int my_power(int n,int k) { if (k-- > 1) n*=my_power(n, k); return n; } int main() { int n = 0, k = 0, ret = 0; scanf("%d%d", &n, &k); ret = my_power(n, k); printf("%d^%d=%d\n", n, k, ret); system("pause"); return 0; }
非递归
#include<stdio.h> int my_power(int n,int k) { int i = 1; while (k-- > 0) i *= n; return i; } int main() { int n = 0, k = 0, ret = 0; scanf("%d%d", &n, &k); ret = my_power(n, k); printf("%d^%d=%d\n", n, k, ret); system("pause"); return 0; }