题意:三柱汉诺塔,每个圆盘每次只能移动到相邻的圆柱上,并且小盘在上,大盘在下,只有最大的盘允许在最上,求n个盘子时移动的次数
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2077
思路:递推式,公式f(n) = 3 * f(n-1) + 2 , ans(n) = f(n-1) + 2
注意点:无
以下为AC代码:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
12683909 | 2015-01-12 00:20:05 | Accepted | 2077 | 0MS | 1212K | 991 B | G++ | luminous11 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
int main()
{
ios::sync_with_stdio( false );
ll num[100] = { 0, 2, 8 };
for ( int i = 3; i < 25; i ++ ){
num[i] = num[i-1] * 3 + 2;
}
int n;
cin >> n;
while ( n -- ){
int k;
cin >> k;
ll ans = num[k-1] + 2;
cout << ans << endl;
}
return 0;
}