您的位置:首页 > 其它

GeeksForGeeks Power(x,n)

2016-02-08 07:16 288 查看
This is a example to use the divide and conquer


Write a program to calculate pow(x,n)

Power(x,n)这里用一个int tem 记录Power(x, n/2),然后将两者相乘得到的结果就可以,所以如果计算Power(x,n)就是计算Power(x,n/2)

然后将结果返回

int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: