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;
    }
  • 相关阅读:
    心境的改变
    php之empty()函数常识性的错误
    php原生之实现图片,文件的下载
    多说,我还欠你一个会员
    开发模块化的初步理解
    Gradle模块化项目中使用了非模块化库的编译方法
    系统架构一一前端技术
    系统架构一一ORM的应用
    系统架构——依赖注入
    WPF下的RibbonApplicationMenu控件自定义
  • 原文地址:https://www.cnblogs.com/jhz033/p/5528587.html
Copyright © 2011-2022 走看看