[ CodeVS冲杯之路 ] P3145

   不充钱,你怎么AC?

       题目:http://codevs.cn/problem/3145/

       经典的汉诺塔问题

       我们移动的时候,如果是最小的1号就可以直接移动,否则先将上面的x-1号先移动到借用塔上,然后将x号移动到目标塔,最后将借用塔上的x-1号移动到目标塔,每次移动都需要借用一个借用塔,此塔为不同于出发塔和目标塔的那一个,递归做即可

       次数的话个人比较懒,直接输出2n-1,就不在移动的时候统计次数了

   

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<algorithm>
 7 #define outp printf("%d from %c to %c
",x,a,b)
 8 using namespace std;
 9 
10 void hanoi(int x,char a,char b,char c)
11 {
12     if (x==1)
13     {
14         outp;
15         return;
16     }
17     hanoi(x-1,a,c,b);
18     outp;
19     hanoi(x-1,c,b,a);
20 }
21 int main()
22 {
23     int n;
24     scanf("%d",&n);
25     printf("%d
",(1<<n)-1);
26     hanoi(n,'A','C','B');
27     return 0;
28 }

  A完这道题,终于上黄金了,吃包辣条庆祝一下

原文地址:https://www.cnblogs.com/hadilo/p/5860192.html