C/C++の宿題を片付けます 94代目
■ このスレッドは過去ログ倉庫に格納されています
0762デフォルトの名無しさん
2007/08/12(日) 18:13:54#include <stdio.h>
#include <string.h>
#define N 128
static int stack[3][N], top[3] = {0}, line;
static void push(int index, int val){ if(top[index] < N) stack[index][top[index]++] = val;}
static int pop(int index){ return top[index] > 0 ? stack[index][--top[index]] : 0;}
static void print_hanoi(void){
int i, j;
puts("A B C");
for(i = line - 1; i >= 0; i--){
for(j = 0; j < 3; j++){
if(top[j] <= i) printf(" ");
else printf("%d ", stack[j][i]);
}
putchar('\n');
}
puts("-------");
}
void hanoi(int n, int from, int work, int to){
if(n > 1) hanoi(n - 1, from, to, work);
push(to, pop(from)), print_hanoi();
if(n > 1) hanoi(n - 1, work, from, to);
}
int main(void){
int i, n;
scanf("%d", &n);
line = n;
for(i = n; i > 0; i--) push(0, i);
print_hanoi(), hanoi(n, 0, 1, 2);
return 0;
}
■ このスレッドは過去ログ倉庫に格納されています