解题思路:
常规思路,直接写的解嘛。
参考代码:
#include<bits/stdc++.h> #define hh ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); using namespace std; typedef unsigned long long ull; int main() { hh; int n; while(cin>>n){ ull ans=1; while(n>1){ ans=(ans+1)*2; n--; } cout<<ans<<endl; } return 0; }
另一种思路:利用数组。
#include<bits/stdc++.h> #define hh ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); using namespace std; typedef unsigned long long ull; int main(){ hh; ull ans[32]={1},n; for(int i=1;i<=32;i++){ ans[i]=(ans[i-1]+1)*2; } while(cin>>n) cout<<ans[n-1]<<endl; return 0; }
这样更快
0.0分
0 人评分
众数问题 (C语言代码)浏览:821 |
C语言程序设计教程(第三版)课后习题6.3 (C++代码)浏览:950 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:575 |
C二级辅导-阶乘数列 (C语言代码)浏览:508 |
大神老白 (C语言代码)浏览:600 |
整数平均值 (C语言代码)浏览:777 |
矩阵转置 (C语言代码)浏览:782 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:579 |
1005答案错误为什么浏览:1975 |
回文数(一) (C语言代码)浏览:1117 |