标签:void span code cin str names class pre 分治
采用递归分治的策略来解决汉诺塔问题
#include <iostream> using namespace std; void Hanoi(int n,char a,char b,char c); void move(int n,char a,char b); int cnt=0; int main() { cout<<"plase enter n:"<<endl; int n; cin>>n; cout << "Hanoi "<<n<<endl; Hanoi(n,‘A‘,‘B‘,‘C‘); cout<<"total move times: "<<cnt<<endl; return 0; } void Hanoi(int n,char a,char b,char c){ if(n==1){ move(n,a,c);cnt++; } else{ Hanoi(n-1,a,c,b); move(n,a,c);cnt++; Hanoi(n-1,b,a,c); } } void move(int n,char a, char b){ cout<<"move number "<<n<<" block from "<<a<<" to "<<b<<endl; }
标签:void span code cin str names class pre 分治
原文地址:https://www.cnblogs.com/joelwang/p/10996336.html