如图所示,打印遍历后的结果。
策略:由外到内,一圈圈的打印,直到没有元素输出。
#include <iostream>
using namespace std;
int print_array_round(int **a, int row_start,int row_end, int col_start, int col_end) {
int count = 0; //本次打印的元素个数
// 右
for(int i=col_start;i<=col_end;++i) {
cout<<a[row_start][i]<<" ";
++count;
}
// 下
for(int i = row_start+1; i<=row_end;++i) {
cout<<a[i][col_end]<<" ";
++count;
}
// 左
for(int i=col_end-1;i>=col_start;--i) {
cout<<a[row_end][i]<<" ";
++count;
}
// 上
for (int i=row_end-1;i>=row_start+1;--i) {
cout<<a[i][col_start]<<" ";
++count;
}
return count;
}
void print_array(int **a, int row,int col) {
int row_start = 0;
int row_end = row-1;
int col_start = 0;
int col_end = col-1;
int count = print_array_round(a,row_start,row_end,col_st