码迷,mamicode.com
首页 > 其他好文 > 详细

快速幂

时间:2018-05-13 15:39:15      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:class   div   space   amp   main   string   math   cstring   end   

 

代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <queue>
#include <string>
#include <cstring>
using namespace std;

double po(double a,int b)
{
    int flag=0;
    if (b<0)
    {
        flag=1;
        b=-b;
    }
    double ans=1;
    while (b!=0)
    {
        if((b&1)==1)
            ans*=a;
        a*=a;
        b>>=1;
    }
    if (flag)
        return 1/ans;
    return ans;
}

int main()
{
    int a,b;
    cin>>a>>b;
    cout<<po(a,b)<<endl;
    
}

输入示例:

3 4

输出示例:

81

快速幂

标签:class   div   space   amp   main   string   math   cstring   end   

原文地址:https://www.cnblogs.com/wangxuelin/p/9021138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!