快速幂:quickpow

 

众所周知,快速幂是优化对数的次方运算的最普遍手段。在学习快速幂的思想时,其分治思想容易让大家用简单的递归实现。

但其实,除了递归之外,更好的方法会是简单的 WHILE循环。下面贴代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

int quickpow(int x,int y)
{
    int n=1;
    while(y!=0)
    {
        if (y&1) n*=x;
        x=x*x;
        y=y>>1;
    }
    
    return n;
}

int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    printf("%d\n",quickpow(a,b));
 } 

*记得要用位运算优化哦QWQ

 

PS:这是我的第一篇博文,在此立下FLAG认真刷题,更新BLOG ,加油做一名有志向的蒟蒻QAQ

posted @ 2016-01-27 21:36  sllr15  阅读(936)  评论(0编辑  收藏  举报