真的没骗你,这道才是简单题 —— 对任意给定的不超过 10 的正整数 n,要求你输出 2
n
。不难吧?
输入格式:
输入在一行中给出一个不超过 10 的正整数 n。
输出格式:
在一行中按照格式 2^n = 计算结果 输出 2
n
的值。
输入样例:
5
输出样例:
2^5 = 32
优化一下用快速幂
#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
int quickpow(int a, int n)
{
int base = a;
int res = 1;
while (n)
{
if (n & 1)
res *= base;
base *= base;
n >>= 1;
}
return res;
}
int main()
{
int n;
cin >> n;
cout << "2^" << n << " = " << quickpow(2, n) << endl;
return 0;
}