C/C++の宿題を片付けます 94代目
■ このスレッドは過去ログ倉庫に格納されています
0741デフォルトの名無しさん
2007/08/11(土) 19:23:19#include <stdio.h>
#define N 128
#define make_stack_set(str) \
int str[N], str##_top = 0; \
void str##_push(int val){ if(str##_top < N) str[str##_top++] = val;} \
int str##_pop(void){ if(str##_top > 0) return str[--str##_top];}
make_stack_set(a); make_stack_set(b); make_stack_set(c);
#define huga(a) if(i >= a##_top) printf(" "); else printf("%d ", a[i])
#define hoge(a, b) if(!strcmp(from, #a) && !strcmp(to, #b)) b##_push(a##_pop())
void print_hanoi(void){
int i, top = (a_top > b_top && a_top > c_top) ? a_top : b_top > c_top ? b_top : c_top;
puts("A B C");
for(i = top - 1; i >= 0; i--){
huga(a); huga(b); huga(c); putchar('\n');
}
puts("---------\n");
}
void hanoi(int n, char *from, char *work, char *to){
if(n > 1) hanoi(n - 1, from, to, work);
hoge(a, b); hoge(a, c); hoge(b, a); hoge(b, c); hoge(c, a); hoge(c, b);
print_hanoi();
if(n > 1) hanoi(n - 1, work, from, to);
}
int main(void){
int i, n;
scanf("%d", &n);
for(i = n; i > 0; i--) a_push(i);
print_hanoi();
hanoi(n, "a", "b", "c");
return 0;
}
■ このスレッドは過去ログ倉庫に格納されています