循环打印,条件是col>2*start && row>2*start;
在从右往左和从下往上打印时,注意条件,否则会多打
<span style="font-size:18px;">#include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <time.h>
using namespace std;
void print(int **num ,int row,int col,int start)
{
int endX=col-1-start;
int endY=row-1-start;
//→
cout<<"→"<<endl;
for(int i=start;i<=endX;i++)
{
//cout<<num[start][i]<<" ";
cout<<*((int*)num + start*col + i)<<" ";
}
cout<<endl;
//↓
cout<<"↓"<<endl;
for(int i=start+1;i<=endY;i++)
cout<<*((int*)num+i*col+endX)<<" ";
cout<<endl;
//←
if(start<endX&& start<endY)
{
cout<<"←"<<endl;
for(int i=endX-1;i>=start;i--)
cout<<*((int *)num+endY*col+i)<<" ";
cout<<endl;
}
//↑
if(start<endX && start<endY-1)
{
cout<<"↑"<<endl;
for(int i=endY-1;i>start;i--)
cout<<*((int *)num+i*col+start)<<" ";
cout<<endl;
}
}
void printM(int **num,int row,int col)
{
if(num==NULL || row<0 || col<0)
return;
int start=0;
while(row>start*2 && col>start*2)
{
print(num,row,col,start);
++start;
cout<<"start="<<start<<endl;
}
}
void main()
{
const int row=5;
const int col=3;
int num[row][col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
num[i][j]=i*row+j;
}
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<num[i][j]<<" ";
}
cout<<endl;
}
cout<<"-------------------------------"<<endl;
printM((int **)num,row,col);
//print((int **)num,row,col,1);
}</span>