先來看一副圖吧:
題目的意思給一個初始值6,列印如形,先仔細分析分析看:
1.圖形是6行6列;
2.從第一行第一列的位置開始,轉圈,數值依次增加;
3.第一列第一行的初始值是1;
進一步分析:
1:起始值:x=0,y=0,direction=right,startValue=1;
2:迴圈條件:若能沿著上一次的方向,則繼續,否則,從right到bottom,從bottom到left,從left到top,從top到right,迴圈迭代;
3:終止條件,找不到direction;
至此,大概有一個思路了,依據當前的位置資訊,找出下一個位置資訊,依次迴圈;
貼個剛寫的代碼:
// .h檔案
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檔案
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>}</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 />}<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 />}<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 />}<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 />}<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 />}<br />break;<br />default:<br />break;<br />}</p><p>return bRet;<br />}
//樣本調用
CPrintDemo oDemo;<br />oDemo.Init(5);<br />oDemo.Sort();<br />oDemo.Print();<br />::printf("/n/n");</p><p>oDemo.Init(6);<br />oDemo.Sort();<br />oDemo.Print();<br />::printf("/n/n");</p><p>oDemo.Init(6, 100);<br />oDemo.Sort();<br />oDemo.Print();<br />::printf("/n/n");
範例程式碼的輸出結果:
只要細細思考一下,算是蠻容易的哈。