Let's take a look at the figure:
The meaning of the question is given to an initial value of 6, which is printed in a shape. First, analyze it carefully and see:
1. The graph contains six rows and six columns;
2. Starting from the position in the first column of the first row, the number increases in turn;
3. the initial value of the first row in the first column is 1;
Further analysis:
1: start value: x = 0, y = 0, direction = right, startvalue = 1;
2: cyclic condition: if the condition can be continued in the previous direction, otherwise, from right to bottom, from bottom to left, from left to top, from top to right, loop iteration;
3: termination condition. No direction is found;
At this point, there is a general idea. Based on the current location information, find the next location information and cycle it in turn;
Post a newly writtenCode:
//. H file
Enum endirection <br/>{< br/> en_left, <br/> en_top, <br/> en_right, <br/> en_bottom, <br/> }; </P> <p> struct sdirectioninfo <br/> {<br/> endirection E; <br/> unsigned int UNX; <br/> unsigned int uny; <br/> int nval; <br/>}; </P> <p> class cprintdemo <br/>{< br/> public: <br/> cprintdemo (); <br/> virtual ~ Cprintdemo (); </P> <p> ///////////////////////////////// //////////////////////////////////////// /<br/> public: <br/> // init Param <br/> void Init (unsigned int unmaxlen, int nstart = 1); <br/> void sort (); <br/> void print (); </P> <p> PRIVATE: <br/> // use input info get the out info <br/> bool _ findnexdirection (endirection & E, int NX, int NY ); </P> <p> PRIVATE: <br/> typedef STD: vector <int> val_container; <br/> val_container m_cval; </P> <p> unsigned int m_unmaxlen; <br/> int m_nstart; </P> <p> };
//. Cpp File
Cprintdemo: cprintdemo () <br/>: m_unmaxlen (0) <br/>, m_nstart (0) <br/>{</P> <p >}</P> <p> cprintdemo ::~ Cprintdemo () <br/>{</P> <p >}</P> <p> void cprintdemo: Init (unsigned int unmaxlen, int nstart) <br/>{< br/> m_unmaxlen = unmaxlen; <br/> m_nstart = nstart; <br/>}</P> <p> void cprintdemo: Sort () <br/>{< br/> If (m_unmaxlen <= 1) <br/>{< br/> return; <br/>}</P> <p> m_cval.assign (m_unmaxlen * m_unmaxlen, 0); </P> <p> bool bfind = false; </P> <p> int Nx = 0; <br/> int ny = 0; <br/> int nval = m_nstart; </P> <p> endirection E = en_right; </P> <p> for (INT I = 0; I <m_unmaxlen * m_unmaxlen; ++ I) <br/> {<br/> m_cval [Ny * m_unmaxlen + NX] = nval; </P> <p> bfind = _ findnexdirection (E, NX, NY ); <br/> If (! Bfind) <br/>{< br/> break; <br/>}</P> <p> switch (E) <br/>{< br/> case en_left: <br/>{< br/> -- NX; <br/>}< br/> break; <br/> case en_top: <br/>{< br/> -- NY; <br/>}< br/> break; <br/> case en_right: <br/>{< br/> + + NX; <br/>}< br/> break; <br/> case en_bottom: <br/>{< br/> + + ny; <br/>}< br/> break; <br/> default: <br/> break; <br/>}< br/> ++ nval; </P> <p >}</P> <p> void cprintdemo :: print () <br/>{< br/> for (size_t I = 0; I <m_cval.size (); ++ I) <br/> {<br/> If (0 = I % m_unmaxlen) <br/>{< br/> printf ("/N"); <br/>}< br/> printf ("% 4D", m_cval [I]); <br/>}</P> <p> bool cprintdemo: :__ findnexdirection (endirection & E, int NX, int NY) <br/>{< br/> bool Bret = false; <br/> switch (e) <br/>{< br/> case en_left: <br/> {<br/> If (0 = NX | 0! = M_cval [Ny * m_unmaxlen + NX-1]) <br/>{< br/> E = en_top; <br/> Bret = true; <br/>}< br/> else if (0 = m_cval [Ny * m_unmaxlen + NX-1]) <br/>{< br/> Bret = true; <br/>}< br/> break; <br/> case en_top: <br/> {<br/> If (0 = ny | 0! = M_cval [(NY-1) * m_unmaxlen + NX]) <br/>{< br/> E = en_right; <br/> Bret = true; <br/>}< br/> else if (0 = m_cval [(NY-1) * m_unmaxlen + NX]) <br/>{< br/> Bret = true; <br/>}< br/> break; <br/> case en_right: <br/>{< br/> If (Nx = m_unmaxlen-1 | 0! = M_cval [Ny * m_unmaxlen + NX + 1]) <br/>{< br/> E = en_bottom; <br/> Bret = true; <br/>}< br/> else if (0 = m_cval [Ny * m_unmaxlen + NX + 1]) <br/>{< br/> Bret = true; <br/>}< br/> break; <br/> case en_bottom: <br/>{< br/> If (NY = m_unmaxlen-1 | 0! = M_cval [(NY + 1) * m_unmaxlen + NX]) <br/>{< br/> E = en_left; <br/> Bret = true; <br/>}< br/> else if (0 = m_cval [(NY + 1) * m_unmaxlen + NX]) <br/>{< br/> Bret = true; <br/>}< br/> break; <br/> default: <br/> break; <br/>}</P> <p> return Bret; <br/>}
// Example call
Cprintdemo odemo; <br/> odemo. init (5); <br/> odemo. sort (); <br/> odemo. print (); <br/>: printf ("/n"); </P> <p> odemo. init (6); <br/> odemo. sort (); <br/> odemo. print (); <br/>: printf ("/n"); </P> <p> odemo. init (6,100); <br/> odemo. sort (); <br/> odemo. print (); <br/>: printf ("/n ");
Output result of the sample code:
It is quite easy to think about it.