Sequential Print matrix
Ideas
Reference Code
#include <iostream>using namespacestd;voidPrintnumasclockwise (inta[][4],intRowintCol) { if(Row <1|| Col <1) return; intup =0, down = row-1, left =0, right = col-1; inti =0; while(Up <= down && left <=Right ) { for(i = left, I <= right; + +)i) cout<< A[up][i] <<" "; ++Up ; for(i = up; I <=; + +)i) cout<< A[i][right] <<" "; --Right ; for(i = right; I >= left;--i) cout<< A[down][i] <<" "; --Down ; for(i = down; I >= up;--i) cout<< A[i][left] <<" "; ++Left ; } cout<<Endl;} intMain () {inta[][4] = {{1,2,3,4}, {5,6,7,8}, {9,Ten, One, A}, { -, -, the, -}}; cout<<sizeof(a)/sizeof(int) <<Endl; Printnumasclockwise (A,4,4);};
Two-dimensional arrays can be replaced by one-dimensional
#include <iostream>using namespacestd;voidPrintnumasclockwise (int*a,intRowintCol) { if(Row <1|| Col <1) return; intup =0, down = row-1, left =0, right = col-1; inti =0; while(Up <= down && left <=Right ) { for(i = left, I <= right; + +)i) cout<< A[up * col + i] <<" "; ++Up ; for(i = up; I <=; + +)i) cout<< A[i * col + right] <<" "; --Right ; for(i = right; I >= left;--i) cout<< A[down * col + i] <<" "; --Down ; for(i = down; I >= up;--i) cout<< A[i * col + left] <<" "; ++Left ; } cout<<Endl;} intMain () {inta[][4] = {{1,2,3,4}, {5,6,7,8}, {9,Ten, One, A}, { -, -, the, -}}; cout<<sizeof(a)/sizeof(int) <<Endl; Printnumasclockwise ((int*) A,4,4);};
Attention
Pay attention to the range of discriminant parameters
if 1 1 ) return;
Results
- 1 2 3 4 8 A - the - - 9 5 6 7 One Ten
Algorithm---Sequential print matrix