Zero Lee的专栏

Calcuate power n of x recursively

Given x and n, calculate its power n:
 1 int power(int x, int n)
 2 {
 3    if (n==0
 4      return 1;
 5    else (n%2==0
 6      return power(x*x, n/2);
 7    else 
 8      return x*power(x*x, n/2);
 9 }
10   
11


posted on 2010-10-18 17:19 Zero Lee 阅读(158) 评论(0)  编辑 收藏 引用 所属分类: Data structure and algorithms


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理