zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #11 B. Jumping Jack 数学

    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;
    }
  • 相关阅读:
    Nexus OSS 3 搭建 Docker & Git LFS 仓库
    YARN FairScheduler
    k8s基本概念及使用
    k8s 基本使用
    10款非常实用的在线网站原型设计工具
    Spark常见问题及性能调优
    spark常见问题处理
    TensorFlow 基本使用
    c语言数组的操作
    在Android开发中遇到的MediaPlayer问题
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5440224.html
Copyright © 2011-2022 走看看