汉诺塔
#include <bits/stdc++.h> using namespace std; int n; /* 把 N- 1个从A经过C移动到B,最后一个从A到C N- 1个在B上,经过A移动到C */ void hanuoti(int n,char A,char B,char C){ if(!n) return; else{ hanuoti(n - 1,A,C,B); //cnt++;记录移动的次数 printf("Move disk %d from %c to %c\n",n,A,C); hanuoti(n - 1,B,A,C); } } int main(){ scanf("%d",&n); hanuoti(n,'A','B','C'); return 0; }
                    
                
                
            
        
浙公网安备 33010602011771号