舞阳网站建设,织梦cms怎么上传wordpress,网络推广文案图片,网站建设合同包含汉诺塔系列2 Time Limit: 1000MS Memory Limit: 65536KBSubmit Statistic DiscussProblem Description 用1,2,...,n表示n个盘子#xff0c;称为1号盘#xff0c;2号盘,...。号数大盘子就大。经典的汉诺塔问 题经常作为一个递归的经典例题存在。可能有人并不知道汉诺塔问题的典…汉诺塔系列2
Time Limit: 1000MS Memory Limit: 65536KBSubmit Statistic DiscussProblem Description 用1,2,...,n表示n个盘子称为1号盘2号盘,...。号数大盘子就大。经典的汉诺塔问 题经常作为一个递归的经典例题存在。可能有人并不知道汉诺塔问题的典故。汉诺塔来源于 印度传说的一个故事上帝创造世界时作了三根金刚石柱子在一根柱子上从下往上按大小 顺序摞着64片黄金圆盘。上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱 子上。并且规定在小圆盘上不能放大圆盘在三根柱子之间一回只能移动一个圆盘。我们 知道最少需要移动2^64-1次.在移动过程中发现有的圆盘移动次数多有的少 。 告之盘 子总数和盘号计算该盘子的移动次数. Input
包含多组数据每组首先输入T,表示有T行数据。每行有两个整数分别表示盘子的数目N(1N60)和盘号k(1kN)。Output
对于每组数据输出一个数表示到达目标时k号盘需要的最少移动数。Example Input 260 13 1 Example Output 5764607523034234884 一开始的超时代码 #includestdio.h
#includestring.h
#includestdlib.h
int a1[100];
int move(int n,char a,char b,char c)
{if(n1){//printf(%c-%c %d\n,a,c,n);a1[n];return 1;}else{move(n-1,a,c,b);//printf(%c-%c %d\n,a,c,n);a1[n];move(n-1,b,a,c);}
}
int main()
{int n,m,t;while(~scanf(%d,t)){scanf(%d%d,n,m);memset(a1,0,sizeof(a1));move(n,a,b,c);printf(%d\n,a1[m]);}
}
后来AC
#include iostream
#include cstdio
#includecmath
using namespace std;
long long int f(int n, int m)
{if(n m) return 1;//如果盘子的序号与盘子总数相同只需要移动一次就行else return f(n, m1)*2;//盘子的序号每减一个就乘2
}
int main()
{int n, k, m;while(cin k){while(k --){cin n m;cout f(n, m) endl;}}return 0;
}后来AC #include iostream
#include cstdio
#includecmath
using namespace std;
long long int f(int n, int m)
{if(n m) return 1;//如果盘子的序号与盘子总数相同只需要移动一次就行else return f(n, m1)*2;//盘子的序号每减一个就乘2
}
int main()
{int n, k, m;while(cin k){while(k --){cin n m;cout f(n, m) endl;}}return 0;
}