打印螺旋数组

循环打印,条件是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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值