3 条题解
- 1
信息
- ID
- 223
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 2
- 标签
- 递交数
- 77
- 已通过
- 50
- 上传者
#include <bits/stdc++.h>
using namespace std;
int n, f[20];
int main()
{
cin >> n;
f[1] = 1;
for (int i = 2; i <= n; i++)
{
f[i] = 2 * f[i - 1] + 1;
}
cout << f[n];
return 0;
}
你这是递推,不是递归
另一个公式:f个移动2*(f-1个移动次数)+1
#include <bits/stdc++.h>
using namespace std;
int times(int x){
if(x==1){
return x;
}else{
return times(x-1)*2+1;
}
}
int main(){
int n;
cin>>n;
cout<<times(n)<<endl;
return 0;
}
公式:f个移动2^f-1次
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
cout<<pow(2,n)-1<<endl;
return 0;
}