zoukankan      html  css  js  c++  java
  • Jumping Jack

    题目描述:

    杰克最近正在努力锻炼他的跳跃技能。当前他正站在X坐标轴原点上。他想跳到坐标(x,0)上,为了达到训练效果,他决定首次跳跃的距离是1,之后每一次跳跃的距离将会比上一次跳跃的距离大1个单位。每一次跳跃,他可以选择往左或者往右跳。他很好奇至少要经过多少次跳跃才能到达终点。

    Input
    单组测试数据。
    输入数据只包含整数x(-10^9<=x<=10^9)。
    Output
    输出杰克到达终点所需要的最少的跳跃次数。

    思路:

    无论正负,步数均相同,因此只考虑一种符号即可(正数和零或负数和零)

    设目标为(x, 0),已走距离为 s,则当 s > x 并且 s - x 为偶数时,只需要在第 (s-x) / 2 步向左走

    当(s-x) 为奇数时 继续走

    代码如下:

    #include<iostream> 
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int main()
    {        
        int enter, begin = 1, time, set = 0, i;
        for ( enter = 0; enter < 21; enter++){    
            if (enter < 0) enter = -enter;        
            set = 0;
            for (i = 0;; i++) {
                set += i;
                if (set == enter) break;            
                if (set > enter && (set - enter) % 2 == 0) break;            
            }
            cout << enter << "->" << i << endl;
        }
        return 0;
    }

    题目出处:

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1615

  • 相关阅读:
    我的友情链接
    我的友情链接
    我的友情链接
    我的友情链接
    我的友情链接
    我的友情链接
    我的友情链接
    以太坊设计与实现:数据结构与对象-账户
    以太坊设计与实现:数据结构与对象-创世区块与配置分析
    以太坊设计与实现:数据结构与对象-链配置
  • 原文地址:https://www.cnblogs.com/Breathmint/p/7594672.html
Copyright © 2011-2022 走看看