網上流傳的一面試題目

來源:互聯網
上載者:User

先來看一副圖吧:

 

題目的意思給一個初始值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");

範例程式碼的輸出結果:

 

 

只要細細思考一下,算是蠻容易的哈。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.