以下是引用lz1091914999在2012-8-3 15:00:48的发言:
#include
#include
int main(void)
{
char format[] = "%xd ";
int snake[500][500], width, i, j, count = 0, x = 0, y = 0;
scanf("%d", &width);
format[1] = (int)log10(width * width) + 1 + '0';
snake[width/2][width/2] = width * width;
for (i = width - 1; i > 0; y -= i - 1, ++x, i -= 2) {
for (j = 0; j < i; ++j)
snake[y][x+j] = ++count;
for (x += i, j = 0; j < i; ++j)
snake[y+j][x] = ++count;
for (y += i, j = 0; j < i; ++j)
snake[y][x-j] = ++count;
for (x -= i, j = 0; j < i; ++j)
snake[y-j][x] = ++count;
}
for (i = 0; i < width; ++i) {
for (j = 0; j < width; ++j)
printf(format, snake[j]);
putchar('\n');
}
return 0;
}
#include
#include
int main(void)
{
char format[] = "%xd ";
int snake[500][500], width, i, j, count = 0, x = 0, y = 0;
scanf("%d", &width);
format[1] = (int)log10(width * width) + 1 + '0';
snake[width/2][width/2] = width * width;
for (i = width - 1; i > 0; y -= i - 1, ++x, i -= 2) {
for (j = 0; j < i; ++j)
snake[y][x+j] = ++count;
for (x += i, j = 0; j < i; ++j)
snake[y+j][x] = ++count;
for (y += i, j = 0; j < i; ++j)
snake[y][x-j] = ++count;
for (x -= i, j = 0; j < i; ++j)
snake[y-j][x] = ++count;
}
for (i = 0; i < width; ++i) {
for (j = 0; j < width; ++j)
printf(format, snake[j]);
putchar('\n');
}
return 0;
}
把思路说出来啊,代码注释一下,这样我们不懂的也可以看看