]> gitweb.ps.run Git - onefile/blob - hanoi.c
add printself.c
[onefile] / hanoi.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void hanoi(char from, char to, int disks)
5 {
6     char third = ('a' + 'b' + 'c') - from - to;
7
8     if (disks == 1)
9     {
10         printf("%c -> %c\n", from, to);
11     }
12     else
13     {
14         hanoi(from, third, disks - 1);
15         printf("%c -> %c\n", from, to);
16         hanoi(third, to, disks - 1);
17     }
18 }
19
20 int main(int argc, char **argv)
21 {
22     if (argc != 4)
23     {
24         printf("Usage: %s <from> <to> <disks>\n", argv[0]);
25         return 1;
26     }
27
28     char from = argv[1][0];
29     char to = argv[2][0];
30     int disks = atoi(argv[3]);
31
32     hanoi(from, to, disks);
33
34     return 0;
35 }