順時針列印矩陣或者 螺旋數組

來源:互聯網
上載者:User

題目:輸入一個矩陣,按照從外向裡以順時針的順序依次列印出每一個數字。
例如:如果輸入如下矩陣:

1             2             3             4
5             6             7             8
9             10            11             12
13            14            15             16
則依次列印出數字1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6,7, 11, 10。


總結規律,找出通項公式這個太複雜了,還是老老實實輸出

方法:

依次:按左-->右,上-->下,右-->左,下-->上的方向交替訪問,如果遇到已經訪問過的元素,那麼就改變訪問的方向。

#include<iostream>using namespace std;int main(){    const int ROW = 4;    const int COL = 4;    int a[ROW][COL]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};    int b[ROW][COL]={0};//記錄此元素有沒有被訪問過,訪問過就置1    int count =0;    int direct = 1;    int row = 0;    int col = -1;    while(count<COL*ROW)    {                       // left to right                       col++;                       while(col<COL && b[row][col]==0)//如果沒有訪問過,就訪問,並且置訪問位 = 1                       {                                   cout<<a[row][col]<<"\t";//                                   b[row][col] = 1;                                   col++;                                   count++;                       }                        col--;//因為上面迴圈退出的話,說明要麼col越界,要麼訪問了已經訪問過的元素,所以col應該往回退1                                              //top to down                       row++;                       while(row<ROW && b[row][col]==0)                       {                                         cout<<a[row][col]<<"\t";                                         b[row][col] = 1;                                         row++;                                         count++;                       }                       row--;                                              //right to left                       col--;                       while(col>=0 && b[row][col]==0)                       {                                         cout<<a[row][col]<<"\t";                                         b[row][col] = 1;                                         col--;                                         count++;                       }                       col++;                                              //down to top                       row--;                       while(row>=0 && b[row][col]==0)                       {                                         cout<<a[row][col]<<"\t";                                         b[row][col] = 1;                                         row--;                                         count++;                       }                       row++;    }    getchar();    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.