You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
502 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}