Hannota solution (CC ++)

zhaozj2021-02-16  48

#include #include

Using namespace std;

Void Hanoi (int N, String Start, String Middle, String End);

Void Move (int N, string start, string end) {cout << "move:" << start << "-> << end << endl;}

Void Exchange (int N, String Start, String Middle, String end) {Hanoi (N - 1, Start, End, Middle); Move (N, Start, End); Hanoi (N - 1, Middle, Start, End) }

Void Hanoi (int N, string start, string middle, string end) {return (n == 1)? Move (n, start, end): Exchange (n, start, middle, end);}

INT main (int Argc, char ** argv) {int N; string start = "@", middle = "#", end = "$";

n = argc> 1? ATOI (Argv [1]): 3; cout << "The solution for n =" << n << endl; hanoi (n, start, middle, end);

Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-15611.html

New Post(0)