List of all available Websheets
Viewing cpp/recursion/hanoi by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Using recursion, create a method <tt>solveTowers()</tt> that prints the solution to the towers of Hanoi problem. |
Public permissions | |
Remarks Comments, history, license, etc. | Copied from problem cpp/recursion/monkey_recback (author: redekopp@usc.edu) |
Engine | |
Template / Reference solution |
using namespace std; void solveTowers(int n, char src, char dst, char alt); int main() { int n; cin >> n; solveTowers(n,'a','b','c'); return 0; } void solveTowers(int n, char src, char dst, char alt) { if(\[n == 1 ]\){ cout << "Move D=" << n << " from " << \[src]\ << " to " << \[dst]\ << endl; } else { \[ solveTowers(n-1, src, alt, dst); cout << "Move D=" << n << " from " << src << " to " << dst << endl; solveTowers(n-1, alt, dst, src); ]\ } } |
C++ test suite json list of stdin/args tests e.g. [{"stdin":"hi", "args":["4", "5"]}, {"stdin":"noargs"}] to just run once with no input use [{}] | [ {"stdin":"3"}, {"stdin":"4"} ] |
Forbidden substrings json list of strings e.g. ["for","while"] | ["for", "while", "do"] |
Solution visibility |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.