C++中有封装的pow()可以直接调用,头文件math,同时对函数进行了重载,支持多种参数格式:
double pow (double base , double exponent); float pow (float base , float exponent);long double pow (long double base, long double exponent); double pow (Type1 base , Type2 exponent); // additional overloads
详情可以参考: 同时给出一种板子:
int mypow(int x, int y) { if (y == 1) return x; int result = 0; int tmp = mypow(x, y/2); if(y&1 != 0) //奇数 { result = x * tmp * tmp; } else { result = tmp * tmp; } return result; }