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\n",i);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/5528587.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值