大蛇程序
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#define M 10
void main()
{
int a[M][M] = {0};
int data = 1;
int N = 0;
scanf("%d",&N);
printf("N is %d\n",N);
for (int i = 0, j = 0, k = 0; k < (N + 1) / 2;k++)
{
while (j<N-k)//控制层数
{
a[i][j] = data;//赋值
j++;//往右边移动
data++;
}
j--;//退回
i++;//转向
while (i<N-k)
{
a[i][j] = data;
i++;
data++;
}
i--;
j--;//退回转向
while (j >k-1 )//减去一层
{
a[i][j] = data;
j--;
data++;
}
j++;
i--;
while (i>k)
{
a[i][j] = data;
i--;
data++;
}
i++;
j++;
}
//打印输出
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
system("pause");
}
转载于:https://blog.51cto.com/lgzkd/1760861