- 论坛徽章:
- 0
|
第一题(绝对没看过别人的答案,本来还想试试,今天刚看到,做好第一题才看到已过了参赛时间)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int k, i, j, n, m;
int nStep;
int nBig;
int nCount;
int *pInts = NULL;
printf("Please input: ");
scanf("%d", &n);
nBig = n*n;
m = 1;
pInts = (int*)malloc(nBig*sizeof(int));
memset((void*)pInts, 0, nBig*sizeof(int));
nStep = n-1;
m = 1;
nCount = nBig;
if(n % 2)
{
nCount--;
pInts[nBig/2] = nBig;
}
for(j=0; ;j++)
{
for(i=j*n+j,k=0; k<nStep; i++,k++)
{
pInts = m++;
if(m > nCount)
goto out;
}
for(i=j*n+n-1-j,k=0; k<nStep; i+=n,k++)
{
pInts = m++;
if(m > nCount)
goto out;
}
for(i=nBig-1-j*n-j,k=0; k<nStep; i--,k++)
{
pInts = m++;
if(m > nCount)
goto out;
}
for(i=n*(n-1-j)+j,k=0; k<nStep; i-=n,k++)
{
pInts = m++;
if(m > nCount)
goto out;
}
nStep-=2;
}
out:
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%-2d, ", *(pInts + i*n + j));
printf("\n");
}
free(pInts);
return 0;
} |
|