我用了模拟来做,太暴力了。我想着就用模拟,不过别人都有很好的办法。可以看这位博主的:https://blog.csdn.net/qq_34202873/article/details/79835619
我倒是侯再仔细看下。
题目是这样的:
第七题
标题:螺旋折线
如图p1.png所示的螺旋折线经过平面上所有整点恰好一次。
对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度。
例如dis(0, 1)=3, dis(-2, -1)=9
给出整点坐标(X, Y),你能计算出dis(X, Y)吗?
【输入格式】
X和Y
对于40%的数据,-1000 <= X, Y <= 1000
对于70%的数据,-100000 <= X, Y <= 100000
对于100%的数据, -1000000000 <= X, Y <= 1000000000
【输出格式】
输出dis(X, Y)
【样例输入】
0 1
【样例输出】
3
#include<iostream>
using namespace std;
int dr[4] = { -1,0,1,0 };
int dc[4] = { 0,1,0,-1 };
int main(void)
{
int a, b,cnt=0;
cin >> a >> b;
int x = 0, y = 0,step=1;
while (true)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < step; j++)
{
cnt++;
x += dr[i];
y += dc[i];
if (a == x && b == y)
{
cout << cnt << endl;
goto loop;
}
}
if (i % 2 == 1)
{
step++;
continue;
}
}
}
loop:;
system("pause");
return 0;
}