Files
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

30 lines
531 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<iostream>
#include <bitset>
using namespace std;
//输出二进制
void printEr(int b) {
cout<<bitset<sizeof(b)*2>(b)<<endl;
}
//快速幂
int poww(int a, int b) {
int ans = 1, base = a;
while (b >0) {
//输出二进制的b
printEr(b);
//快速幂的核心算法
if (b & 1) //例如一个数 & 1 的结果就是取二进制的最末位。最后一位如果是1表示是奇数需要单独乘一次
ans *= base;
base *= base;
//右移一位可以理解为除以2
b >>= 1;
}
return ans;
}
int main() {
cout << poww(2,5);
return 0;
}