A

#include<bits/stdc++.h>
using namespace std;
/*
直接针对平方来找
每次走的是个折线
*/
pair<int,int> work(int x)
{
    int y=(int)sqrt(x*1.0+0.5);
    pair<int,int>ans;
    if(y&1&&y*y==x)
    {
        ans.first=ans.second=(-(y-1)>>1);
        return ans;
    }
    if(!(y&1)&&y*y==x)
    {
        ans.first=(y/2);
        ans.second=(y/2)-1;
        return ans;
    }
    if(y&1)
    {
        ans.first=ans.second=(-(y-1)>>1);
        if(x-y*y-1<=y)
        {
            ans.second--;
            ans.first+=(x-y*y-1);
            return ans;
        }
        else
        {
            ans.first+=y;
            ans.second+=(x-y*y-y-2);
            return ans;
        }
    }
    else
    {
        ans.first=y/2;
        ans.second=y/2-1;
        if(x-y*y-1<=y)
        {
            ans.second++;
            ans.first-=(x-y*y-1);
            return ans;
        }
        else
        {
            ans.first-=y;
            ans.second-=(x-y*y-y-2);
            return ans;
        }
    }
}
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    pair<int,int> a1,a2;
    a1=work(a);
    a2=work(b);
    printf("%d
",abs(a1.first-a2.first)+abs(a1.second-a2.second));
    return 0;
}
原文地址:https://www.cnblogs.com/zhangzhenjun/p/11629654.html