http://oj.ecustacm.cn/problem.php?id=1372
模拟题:找规律。
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int x;
int y;
long long ans;
int main(void)
{
cin>>x>>y;
if(abs(x)<=y&&y>=0)//上
{
if(x>=0)
ans=(long long)(2*y)*(2*y)-(y-x);//上右
else
ans=(long long)(2*y)*(2*y-1)+(y-abs(x));//上左
}
if(abs(y)<=x&&x>=0)//左
{
if(y>=0)
ans=(long long)(2*x)*(2*x)+(x-y);
else
ans=(long long)(2*x)*(2*x+1)-(x-abs(y));
}
if(abs(x)<=abs(y)+1&&y<0)//下
{
if(x>=0)
ans=(long long)(2*abs(y))*(2*abs(y)+1)+(abs(y)-x);
else
ans=(long long)(2*(abs(y)+1)-1)*(2*(abs(y)+1)-1)-(abs(y)+1-abs(x));
}
if(abs(y)<=abs(x)-1&&x<0)//右
{
if(y>=0)
ans=(long long)(2*abs(x))*(2*abs(x)-1)-(abs(x)-y);
else
ans=(long long)(2*abs(x)-1)*(2*abs(x)-1)+(abs(x)-1-abs(y));
}
cout<<ans<<endl;
return 0;
}
下面是y总写的,真的简洁,感觉自己写的好垃圾。
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int main()
{
int x, y;
cin >> x >> y;
if (abs(x) <= y) // 在上方
{
int n = y;
cout << (LL)(2 * n - 1) * (2 * n) + x - (-n) << endl;
}
else if (abs(y) <= x) // 在右方
{
int n = x;
cout << (LL)(2 * n) * (2 * n) + n - y << endl;
}
else if (abs(x) <= abs(y) + 1 && y < 0) // 在下方
{
int n = abs(y);
cout << (LL)(2 * n) * (2 * n + 1) + n - x << endl;
}
else // 在左方
{
int n = abs(x);
cout << (LL)(2 * n - 1) * (2 * n - 1) + y - (-n + 1) << endl;
}
return 0;
}