面试宝典中用C++实现循环队列
时间:2014-11-04 19:40:33
收藏:0
阅读:289
清楚原理后很简单
#include<iostream>
using namespace std;
int abs(int a)
{
return (a>=0)?a:-a;
}
int max(int a,int b)
{
return (abs(a)>=abs(b))?abs(a):abs(b);
}
int foo(int x,int y)
{ //int u;
int a=abs(x);
int b=abs(y);
int t=max(a,b);
int v=(2*t-1)*(2*t-1);
int u=v;
if(y==-t)
{
u=u+7*t+x;
}
else if(x==t)
{
u=u+t+y;
}
else if(y==t)
{
u=u+3*t-x;
}
else
{
u=u+5*t-y;
}
return u;
}
int main()
{ // cout<<foo(1,0)<<endl;
for(int y=-4;y<=4;++y)
{
for(int x=-4;x<=4;++x)
{
cout<<foo(x,y)<<'\t';
}
cout<<endl;
}
system("pause");
return 0;
}
评论(0)