codeforces 11 B.Jumping Jack 想法题

B. Jumping Jack

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.

Examples
input
2
output
3
input
6
output
3
input
0
output
0
题意:给你一个点,你从0开始走,每次你可以选择向左或者向右走,从1开始每次必须加一步,问你走到那个点的最小步数;
思路:小于0的话就改成大于0的;首先那些步数相加一定要大于那个点的位置;和减去那个点的位置相差为偶数的话就可以到达,从小判断,详见代码;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
#define pi 4*atan(1)
//#pragma comment(linker, "/STACK:102400000,102400000")
int a[100010];
int main()
{
    int x,y,z,i,t;
    for(i=0;i<50000;i++)
    {
        if(i%2==0)
        a[i]=i/2*(i+1);
        else
        a[i]=(i+1)/2*i;
    }
    while(~scanf("%d",&x))
    {
        if(x<0)x=-x;
        for(i=0;;i++)
        if(a[i]>=x&&(a[i]-x)%2==0)
        break;
        printf("%d
",i);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/5528587.html