zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    docker 容器与主机之间的数据copy
    vim 中如何快速注释和取消注释
    java查找字符中的某个内容并替换
    linux正则表达式
    数据流重定向与管道命令
    linux杂七杂八
    linux变量
    redis常用命令操作
    redis基本操作介绍
    redis数据结构
  • 原文地址:https://www.cnblogs.com/jhz033/p/5528587.html
Copyright © 2011-2022 走看看