Delta-wave 谁能告诉我拿错啦
A triangle field is numbered with successive integers in the way shown on the picture below. Delta-wave (图百度一下,搜“Delta-wave”就有,麻烦了 )
The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell to cell through vertices. The number of edges the traveller passes makes the length of the traveller's route.
Write the program to determine the length of the shortest route connecting cells with numbers N and M.
Input
Input contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).
Output
Output should contain the length of the shortest route.
Sample Input
6 12
Sample Output
3
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
int delta_wave(int n,int *p)
{
int i=1,count=0,count1=-1;
if(n==1)
{
p[0]=0,p[1]=0,p[2]=0;
return *p;
}
while(1)
{
count++;
n-=i;
i+=2;
if(n<=i) break;
}
p[2]=count1=count;
if(n&1)
count1*=2;
else
count1=count1*2-1;
n=n-(count+1);
p[0]=n,p[1]=count1;
return *p;
}
void main()
{
//freopen("a.txt","r",stdin);
int N,M;
scanf("%d %d",&N,&M);
int t=-35;
int a[3],b[3];
if(N>M) N^=M^=N^=M;
delta_wave(N,a),delta_wave(M,b);
if(a[0]==b[0])
{
t=(a[1]-b[1]);
if(t<0) t=-t;
printf("%d\n",t);
}
else
{
// printf("a[2]=%d b[2]=%d ",a[2],b[2]);
//printf("a[0]=%d b[0]=%d ",a[0],b[0]);
int t2=a[2]-b[2],t0=a[0]-b[0];
if(t2<0) t2=-t2;
if(t0<0) t0=-t0;
t=t2+t0;
printf("%d\n",t);
}
}