這種爛題我再也不想見到了!!
比模擬題還討厭!!
題目大意:
根據題目給出的各個數的位置,求某兩個數之間的位置。
解題思路:
由給出的位置可以找到一個規律,二維的六個象限的座標系,計算座標然後計算距離就行。
下面是代碼:
#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define iabs(x) ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define min( x, y ) ( ((x) < (y)) ? (x) : (y) )
using namespace std;
void cal(int num,int &x,int &y)
{
int n=0;
while(1+3*n*(n+1)<num)n++; //當不再第n圈範圍內時
if(n==0)
{
x=0;
y=0;
return;
} //當屬於第0圈直接返回
num=num-1-3*n*(n-1); //計算num是第n圈的第幾個數
int rou=(num-1)/n; //計算屬於第幾象限
int cnt=(num-1)%n+1; //計算是第幾象限的第幾個數
if(rou==0)x=cnt,y=n-cnt;
else if(rou==1)x=n,y=-cnt;
else if(rou==2)x=n-cnt,y=-n;
else if(rou==3)x=-cnt,y=cnt-n;
else if(rou==4)x=-n,y=cnt;
else if(rou==5)x=cnt-n,y=n;
return ;
}
int main()
{
int a,b,xa,ya,xb,yb,ans;
while(scanf("%d%d",&a,&b),a||b)
{
cal(a,xa,ya);
cal(b,xb,yb);
if((xb-xa)*(yb-ya)<=0)
{
ans=iabs(xa-xb);
if(iabs(xa-xb)<iabs(ya-yb))ans+=iabs(ya-yb)-iabs(xa-xb);
}
else
{
ans=iabs(ya-yb)+iabs(xa-xb);
}
printf("The distance between cells %d and %d is %d.\n",a,b,ans);
}
return 0;
}