螺旋队列问题(由内自外旋转)

本文介绍了一个用于二维坐标系中特定模式下整数坐标到唯一数值转换的算法。该算法通过定义一个螺旋状增长的整数分布模式,实现了从任意整数坐标到其对应序列号的映射,并提供了一个类实现,包括获取最大绝对值、获取绝对值、获取坐标对应的数值等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

21 22.....................

20 7 8 9 10

19 6 1 2 11

18 5 4 3 12

17 16 15 14 13


#include<iostream>
#include<stdio.h>
using namespace std;

class InsideSpin 
{
public:
	InsideSpin();
	int Max(int x,int y);
	int Abs(int a);
	int GetNum(int x,int y);
	void Print(int x,int y);
};
InsideSpin::InsideSpin()
{}
int InsideSpin::Max(int x,int y)
{
	return Abs(x)>Abs(y)?Abs(x):Abs(y);
}

int InsideSpin::Abs(int a)
{
	return abs(a);
	//return a>0?a:-a;
}

int InsideSpin::GetNum(int x,int y)
{
	int t = Max(x,y);
	int u = t+t;
	int v = u-1;
	 v = v* v+u;
	if(x==-t)//向x的负方向增长
	{
		v += u + t - y;
	}
	else if(y==-t)//向y的负方向增长
	{
		v +=3 * u+ x - t;
	}
	else if(y==t)//向y的正方向增长
	{
		v += t-x;
	}
	else//向x的正方向增长
	{
		v +=y -t;
	}
	return v;
}
void InsideSpin::Print(int x0,int y0)
{
	int x,y;
	cout<<"*****************打印从坐标(0,0)到坐标("<<x0<<","<<y0<<")之间的所有的数****************"<<endl;
	
 	for(y =-y0;y<=y0;y++)
	{
		cout<<"	";
		for(x = -x0;x<=x0;x++)
		{
			int v= GetNum(x,y);
			cout<<v<<"	";
		}
		cout<<endl;
	}
	cout<<"***************************************************************************"<<endl;
}

int main()
{
	InsideSpin s;
	s.Print(4,4);
	int x,y;
	cout<<"依次输入x,y的值"<<endl;
	while(!cin.eof(),cin>>x,cin>>y)
	{
		cout<<s.GetNum(x,y)<<endl;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值