#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.
2
3
6
3
0
0