[Data structures]Hanoi
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1024 char stack[MAX][255]; int top = -1; void printStack() { int i; for(i=top; i>=0; i--){ printf("%s\n", stack[i]); } printf("\n"); } void hanoi(int n, char x, char y, char z) { printStack(); if(n==1){ printf("%c -> %c\n", x, z); }else{ sprintf(stack[++top], "%d, %d, %c, %c, %c", 6, n-1, x, z, y); hanoi(n-1, x, z, y); printStack(); printf("%c -> %c\n", x, z); sprintf(stack[++top], "%d, %d, %c, %c, %c", 8, n-1, y, x, z); hanoi(n-1, y, x, z); printStack(); } top--; } void main() { printf("log.anycle.com\n\n"); printf("Hanoi:\n"); sprintf(stack[++top], "%d, %d, %c, %c, %c", 0, 3, 'a', 'b', 'c'); hanoi(3, 'a', 'b', 'c'); printf("\n"); }
日历
最新微语
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
- 时间不可以倒流,但空间可以
2017-08-01 09:03
- 含羞草、电磁炮;汽车工业革命
2017-05-23 22:51
- 那个点子页面加几点:
去中心化的物联网通信协议
2017-05-09 22:13
- 有一种人怀疑阴阳的存在,另有一种人会怀疑1+1=2的正确性……
2017-03-01 17:08
分类
最新评论
- terwixonse
I am from Slovenia. ... - terwixonse
Help. I am looking f... - 萧
Set environment args... - 萧
在新电脑上: ./configure ... - 萧
lib/libQtGui.so: und... - 萧
If there is error ab... - nicholas
If I add option '-no... - 萧
"make clean" before ... - nicholas
如果没有/dev/event0 那估计... - nicholas
../libtool: eval: li...
发表评论: