#include<cstdio>
#include<cstring>
#include<string>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
int main()
{
int x;
scanf("%d",&x);
x=x<0?-x:x;
int n=0;
int s=0;
while(1){
s=n*(n+1)/2;
if(s==x) break;
if(s>x){
int c=s-x;
if(c%2==0){
break;
}
}
n++;
}
printf("%d\n",n);
return 0;
}
首先,你一直往前跳,如果刚好能跳到目标肯定是最好的,如果不能,你就应该跳到第一个离目标的为偶数的地方,x-y=n。像右多走了n步,那么,你就应该在n/2的时候,往左。
Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point x. In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach x.
The input data consists of only one integer x ( - 109 ≤ x ≤ 109).
Output the minimal number of jumps that Jack requires to reach x.
Input
2
Output
3
Input
6
Output
3
Input
0
Output
0
本文介绍了一个用于计算到达指定位置所需最小跳跃次数的算法。通过逐步增加跳跃距离并适时调整方向,确保了用最少的步骤达到目标。适用于训练跳跃技能或解决类似数学问题。
781

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



