原题目是这样的:
Suppose we got a integer named size from input. size meet the rule:
size%2 ==1.
We use this size to build a square blocks, and we start from the center of block to go through all the array in clock wise. Please print the footstep sequence by number, please note we use asterisk to divide two neighbor numbers.
For example, we get size =3 from standard input, we should print following graph to standard output
7*8*9
6*1*2
5*4*3
For example, we get size =5 from standard input,we should print following graph to standard output
21*22*23*24*25
20*7*8*9*10
19*6*1*2*11
18*5*4*3*12
17*16*15*14*13
有点像阿基米德螺旋线:
给一个代码的例子:
代码通过g++ 4.7.2、vs2012、vs2010编译
#include<iostream>
using namespace std;
int main()
{
int size = 0;
cin>>size;
if(size %2 == 0)
{
cout<<"size must be can % 2 == 1"<<endl;
return -1;
}
int n = size*size;
int **a = new int *[size] ;
int i = 0, j = 0;
for(i = 0; i != size; ++i)
a[i] = new int[size];
int step = 1;
while(step <= size/2+1)
{
i = step -1;
j = size - step;
while(j > i)
a[i][j--] = n--;
while(i < (size - step))
a[i++][j] = n--;
while(j < i)
a[i][j++] = n--;
while(i >=step)
a[i--][j] = n--;
step++;
}
a[i][j] = 1;
for(i = 0; i != size; ++i)
{
j = 0;
while(true)
{
cout << a[i][j++];
if (j != size)
{
cout << " * ";
}
else
{
break;
}
}
cout << endl;
}
for(i = 0; i != size; ++i)
{
if (a[i])
{
delete[] a[i];
a[i] = NULL;
}
}
if (a)
{
delete a;
a = NULL;
}
}