计算指数(记一个关于printf()和pow()的细节:

 

真的没骗你,这道才是简单题 —— 对任意给定的不超过10的正整数n,要求你输出2^n 。

输入格式:

输入在一行中给出一个不超过10的正整数n。

输出格式:

在一行中按照格式 2^n = 计算结果 输出2^n的值。

输入样例:

5
 

输出样例:

2^5 = 32

 1 #include <cstdio>
 2 #include <cmath>
 3 using namespace std;
 4 int main() {
 5     int n;
 6     scanf("%d", &n);
 7     printf("2^%d = %d", n, (int)pow(2, n));  
 8    //pow返回一个double型,如果不想用int强制转换可以写成“printf("2^n = %.0f", n, pow(2, n));”也能过
 9     return 0;
10 }

 

posted @ 2020-04-27 19:58  lavaicer  阅读(246)  评论(0)    收藏  举报