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;
}
}