diff options
| -rw-r--r-- | hanoi.c | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,35 @@ +#include <stdio.h> +#include <stdlib.h> + +void hanoi(char from, char to, int disks) +{ + char third = ('a' + 'b' + 'c') - from - to; + + if (disks == 1) + { + printf("%c -> %c\n", from, to); + } + else + { + hanoi(from, third, disks - 1); + printf("%c -> %c\n", from, to); + hanoi(third, to, disks - 1); + } +} + +int main(int argc, char **argv) +{ + if (argc != 4) + { + printf("Usage: %s <from> <to> <disks>\n", argv[0]); + return 1; + } + + char from = argv[1][0]; + char to = argv[2][0]; + int disks = atoi(argv[3]); + + hanoi(from, to, disks); + + return 0; +} |
