以右下角对角线上的点为参照点,测算给定的点到参照点要走的距离
C++ 代码
#include <iostream>
#include <cstdlib>
//等差数列求和
#define sum(a0, n, d) (2*(a0)+((n)-1)*(d))*(n)/2
typedef long long LL;
using namespace std;
int main() {
LL X, Y;
// while(1) {
cin >> X >> Y;
LL d = 0;//距离
LL n = 0;//第几圈
if (Y > 0 && abs(X) <= Y) {
n = Y;
d = Y - X + 2 * Y;
} else if (X > 0 && abs(Y) <= X) {
n = X;
d = Y + X;
} else if (Y <= 0 && X >= Y - 1 && X <= -Y) {
n = -Y;
d = -(-Y - X);
} else if (X < 0 && Y >= X + 1 && Y <= -X) {
n = -X-1;
d = -(Y - X - 1 - 2 * X - 1);
}
cout << sum(1, 2 * n, 1) * 2 - d << endl;
// }
return 0;
}