快速幂
时间:2020-03-02 21:49:37
收藏:0
阅读:62
思路:将k看作二进制数,每次与1,若得到结果为1,就相乘.
#include<iostream>
using namespace std;
int main()
{
int n = 2, k = 8;
int res = 1;
cout<<n<<"的"<<k<<"次方等于:";
while(k)
{
if(k & 1)
{
res *= n;
}
n *= n;
k >>= 1;
}
cout<<res<<endl;
return 0;
}
原文:https://blog.51cto.com/14472348/2474986
评论(0)