1 each time the outer ring of a traversal may be divided into four steps, the upper left to the upper right - "upper right to the lower right -" lower left to lower right - "lower left to the upper left 2 then loops the above step until the matrix has been printed up to 3 by ai, aj to controlling the number of layers . 4 . 5 #include <the iostream> . 6 #include <Vector> . 7 #include <algorithm> . 8 . 9 the using namespace STD; 10 . 11 int main () 12 is { 13 is // Vector <Vector <int>> V (. 4, Vector <int> (. 4)); 14 int n-, m; 15 CIN >> >> n- m; 16 Vector <Vector < int >> V; . 17 vector<int> d; 18 for(int i=0;i<n;i++){ 19 for(int j=0;j<m;j++){ 20 int flag; 21 cin>>flag; 22 d.push_back(flag); 23 } 24 v.push_back(d); 25 d.clear(); 26 } 27 int maxi =v.size(),maxj =v[0].size(); 28 int cot =0; 29 int ai,aj,i,j; 30 ai =0; 31 aj =0; 32 cout<<endl; 33 while(1){ 34 for(j = aj ;j < maxj-aj; j++){ 35 cout<<v[ai][j]<<" "; 36 cot++; 37 } 38 if(cot == maxi*maxj) break; 39 for(i = ai+1 ;i < maxi - ai ; i++){ 40 cout<<v[i][maxj-aj-1]<<" "; 41 cot++; 42 } 43 if(cot == maxi*maxj) break; 44 for(j = maxj-2-aj; j>=aj;j--){ 45 cout<<v[maxi-ai-1][j]<<" "; 46 cot++; 47 } 48 if(cot == maxi*maxj) break; 49 for(i = maxi - ai -2 ;i>= ai+1 ; i--){ 50 cout<<v[i][aj]<<" "; 51 cot++; 52 } 53 if(cot == maxi*maxj) break; 54 ai ++; 55 aj ++; 56 } 57 return 0; 58 }
acm it clockwise print matrix
Guess you like
Origin www.cnblogs.com/sanshiya/p/12210314.html
Recommended
Ranking