最近在看MIT的算法公开课,讲到分治法的求X的N次方时,只提供了数学思想,于是自己把代码写了下,虽然很简单,还是想动手写一写。

  int powerN(int x,int n){

if(n==0){

  return 1;

}

int childN = n/2;

int result;

result = powerN(x,childN);

if(n&1){

  return result*result;

}  

else{

  return result*result*x;

}

  }

posted on 2013-09-29 00:23  LifeStudio  阅读(1360)  评论(0编辑  收藏  举报