CodeForces - 11B ---思路题

本文介绍了一个用于计算到达指定位置所需最小跳跃次数的算法。通过逐步增加跳跃距离并适时调整方向,确保了用最少的步骤达到目标。适用于训练跳跃技能或解决类似数学问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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.

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.

Example
Input
2
Output
3
Input
6
Output
3
Input
0
Output
0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值