ぼるじょあがC/C++の宿題を片づけますYO! 48代目
■ このスレッドは過去ログ倉庫に格納されています
0394デフォルトの名無しさん
2005/07/24(日) 19:34:092番。前作った奴の焼き直し。コピペすれば動くはず。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int* pos(int* data, int x, int y, int width) {return data + (width + 1) * y + x;}
int main(void) {
int size, x = 0, y = 0, i, j, *matrix, counter, digit;
scanf("%d", &size);
counter = size * size; digit = (int)log10(size) + 2.5;
matrix = (int*)malloc(sizeof(int) * (size + 1) * size);
memset(matrix, -1, sizeof(int) * (size + 1) * size);
*pos(matrix, x, y, size) = counter;
for(; ; ) {
while (++x < size && *pos(matrix, x, y, size) == -1) *pos(matrix, x, y, size) = --counter;
*pos(matrix, --x, y, size) = counter; if (*pos(matrix, x, y + 1, size) != -1) break;
while (++y < size && *pos(matrix, x, y, size) == -1) *pos(matrix, x, y, size) = --counter;
*pos(matrix, x, --y, size) = counter; if (*pos(matrix, x - 1, y, size) != -1) break;
while (--x >= 0 && *pos(matrix, x, y, size) == -1) *pos(matrix, x, y, size) = --counter;
*pos(matrix, ++x, y, size) = counter; if (*pos(matrix, x, y - 1, size) != -1) break;
while (--y >= 0 && *pos(matrix, x, y, size) == -1) *pos(matrix, x, y, size) = --counter;
*pos(matrix, x, ++y, size) = counter; if (*pos(matrix, x + 1, y, size) != -1) break;
}
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) printf("%*d", digit, *pos(matrix, j, i, size));
printf("\n");
}
free(matrix);
return 0;
}
■ このスレッドは過去ログ倉庫に格納されています