快速幂算法

来源:互联网 发布:政务大数据平台 编辑:程序博客网 时间:2024/06/16 13:18
#include<iostream>
using namespace std;
int pw(int a,int b)
{
int res=1;
while(b)
{
if(b%2==1)
res*=a;
a*=a;
b/=2;
}
return res;
}
int main()
{
int a,b;
cin>>a>>b;
cout<<pw(a,b)<<endl;
return 0; 
}
//int pw2(int a,int b) 二进制 
//{
// int res=1;
// while(b)
// {
// if(b&1)
// res*=a;
// a*=a;
// b>>=1;
// }
// return res;
//}

原创粉丝点击