Codeforces Beta Round #11 B. Jumping Jack 数学

本文解析了 Codeforces 上的问题 B.JumpingJack。该问题要求计算从原点出发到达目标点所需的最少跳跃次数,每次跳跃的距离递增。文章提供了问题描述、输入输出示例及解决方案,并附带 C++ 实现代码。
摘要由CSDN通过智能技术生成

B. Jumping Jack

题目连接:

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值