treesummaryrefslogcommitdiff
diff options
context:
space:
mode:
authorpatrick-scho2025-05-30 22:57:12 +0200
committerpatrick-scho2025-05-30 22:57:12 +0200
commit2b3f2bfe27d231b7d140785520597dffed826a35 (patch)
tree11e152b328a351a1f4ab05e9bbd415b956be26b2
parent6f2e28e44c3d1552a12b4e3d92f9d4788d5aab09 (diff)
downloadonefile-2b3f2bfe27d231b7d140785520597dffed826a35.tar.gz
onefile-2b3f2bfe27d231b7d140785520597dffed826a35.zip
add hanoi.c
-rw-r--r--hanoi.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/hanoi.c b/hanoi.c
new file mode 100644
index 0000000..ddbe4db
--- /dev/null
+++ b/hanoi.c
@@ -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;
+}