类似汉诺塔的题
大佬推导

#include<bits/stdc++.h>
using namespace std;
int n;
int hnt(int n)
{
if(n == 1) return 2;
else return 3 * hnt(n-1)+ 2 ;
}
int main()
{
while(~scanf("%d", &n))
{
cout << hnt(n) << endl;
cout << pow(3,n) - 1 << endl;
}
return 0;
}
本文探讨了经典的汉诺塔问题的递归解决方案,并通过代码展示了如何用C++实现。重点介绍了当n递增时,递归调用次数和总操作数的关系。同时,讲解了算法的时间复杂度为O(2^n)。
1700

被折叠的 条评论
为什么被折叠?



