专栏文章
题解:P1096 [NOIP2007 普及组] Hanoi 双塔问题
P1096题解参与者 2已保存评论 1
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @miqfsvmi
- 此快照首次捕获于
- 2025/12/04 04:05 3 个月前
- 此快照最后确认于
- 2025/12/04 04:05 3 个月前
思路:
这题分享 C++ 里很简单的字符串做法。
首先,我们可以算出递推式 。
那么,如果按照这个递推式,你能拿到不错的 分。
CPP#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
n--;
printf("%d\n", 2 * ((int)pow(2, n + 1) - 1));
return 0;
}
作为第四题能拿到这样的成绩其实也不错了,但是想拿满分也很简单。
只要用到 C++ 里的字符串库就可以了。
CPPs << fixed << pow(2.0L, n + 1)
这一句,是根据递推式 来写的。
CPPa = s.str()
这一句,是将算出的 存进 中,才得输出。
CPPa[a.length() - 1] -= 2;
这一句是把相对应的汉诺塔个数的那一个,减掉多余的 ,这样才能对应我们的递推式。
code:
CPP#include <bits/stdc++.h>
using namespace std;
int n;
string a;
stringstream s;
int main() {
cin >> n;
s.precision(0);
s << fixed << pow(2.0L, n + 1);
a = s.str();
a[a.length() - 1] -= 2;
cout << a << endl;
return 0;
}
相关推荐
评论
共 1 条评论,欢迎与作者交流。
正在加载评论...