题目连接:
http://www.codeforces.com/contest/11/problem/B
Description
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.
Input
The input data consists of only one integer x ( - 109 ≤ x ≤ 109).
Output
Output the minimal number of jumps that Jack requires to reach x.
Sample Input
2
Sample Output
3
Hint
题意
这个人在0点这个位置,每次可以选择往左边或者往右边跳
每次跳了之后,他的跳跃力都会增加1
问你跳到他想要的位置,最小需要跳多少次?
题解:
最简单的就是如果终点一直能够跳过去就到就好了。
如果跳过了的话,如果跳过的距离是偶数的话,可以通过使得(now-x)/2这一步往远离终点的方向跳就好了
如果是奇数的话,那就再跳几步就好了。
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long x;
scanf("%lld",&x);
if(x<0)x=-x;
int res = 0;
int tot = 0;
while(tot<x||(tot-x)%2)
{
res++;
tot+=res;
}
cout<<res<<endl;
}