Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
Input
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
Output
Print a single integer, showing how many times Valera has to turn.
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
int p,x,y,i,k;
while(cin>>x>>y){
if(x==0&&y==0)
k=0;
else{
p=max(abs(x),abs(y));
k=(p-1)*4;
if(x==p&&y>1-p&&y<=p)
k+=1;
else if(y==p&&x>=-p&&x<=p)
k+=2;
else if(x==-p&&y>=-p&&y<=p)
k+=3;
else if(y<0&&x>y&&x<=-y)
k+=4;
}
cout<<k<<endl;
}
return 0;
}
博客内容概述:这篇博客讨论了平面直角坐标系中的一条无限螺旋路径,它经过所有整数点。马Valera从原点出发,想要沿着这条螺旋路径走到点(x, y)。由于马有四条腿,转弯比较困难。博客提供了输入x和y的值,计算Valera在到达(x, y)过程中需要转弯的次数。"
82910356,7816798,K-means聚类:如何选择最佳k值,"['数据挖掘', '机器学习', '聚类分析', 'K-means算法']
301

被折叠的 条评论
为什么被折叠?



