给定一个整数 X。任务是找到从零开始到达数轴中点 X 的跳跃次数。
注意:第一次跳跃的长度可以是一个单位,每次连续跳跃的长度都将比前一次跳跃的长度长一个单位。每次跳跃都允许向左或向右移动。
例子:
Input : X = 8
Output : 4
Explanation :
0 -> -1 -> 1 -> 4-> 8 are possible stages.
Input : X = 9
Output : 5
Explanation :
0 -> -1 -> -3 -> 0 -> 4-> 9 are
possible stages
方法:仔细观察,可以很容易地说:
- 如果您一直朝正确的方向跳跃,那么在跳跃n次后,您将到达p = 1 + 2 + 3 + 4 + … + n 点。
- 如果您在第 k 次跳跃时不是向右跳,而是向左跳,那么您将处于p – 2k点。
- 此外,通过仔细选择向左跳和向右跳,在 n 次跳跃后,您可以在 n * (n + 1) / 2 和 – (n * (n + 1) / 2) 之间的任何点与 n * (n + 1) / 2 相同的奇偶校验。
牢记以上几点,你要做的就是模拟跳跃的过程,总是向右跳跃,如果在某个点,你已经到达了一个与X具有相同奇偶校验的点,并且处于或超过X,你会有你的答案。
下面是上述方法的实现:
C++
// C++ program to find the number of jumps
// to reach X in the number line from zero
#include
using namespace std;
// Utility function to calculate sum
// of numbers from 1 to x
int getsum(int x)
{
return (x * (x + 1)) / 2;
}
// Function to find the number of jumps
// to reach X in the number line from zero
int countJumps(int n)
{
// First make number positive
// Answer will be same either it is
// Positive or negative
n = abs(n);
// To store required answer
int ans = 0;
// Continue till number is lesser or not in same parity
while (getsum(ans) < n or (getsum(ans) - n) & 1)
ans++;
// Return the required answer
return ans;
}
// Driver code
int main()
{
int n = 9;
cout << countJumps(n);
return 0;
}
Java
// Java program to find the number of jumps
// to reach X in the number line from zero
class GFG
{
// Utility function to calculate sum
// of numbers from 1 to x
static int getsum(int x)
{
return (x * (x + 1)) / 2;
}
// Function to find the number of jumps
// to reach X in the number line from zero
static int countJumps(int n)
{
// First make number positive
// Answer will be same either it is
// Positive or negative
n = Math.abs(n);
// To store required answer
int ans = 0;
// Continue till number is lesser
// or not in same parity
while (getsum(ans) < n ||
((getsum(ans) - n) & 1) > 0)
ans++;
// Return the required answer
return ans;
}
// Driver code
public static void main(String args[])
{
int n = 9;
System.out.println(countJumps(n));
}
}
// This code is contributed by Ryuga
Python3
# Python 3 program to find the number of jumps
# to reach X in the number line from zero
# Utility function to calculate sum
# of numbers from 1 to x
def getsum(x):
return int((x * (x + 1)) / 2)
# Function to find the number of jumps
# to reach X in the number line from zero
def countJumps(n):
# First make number positive
# Answer will be same either it is
# Positive or negative
n = abs(n)
# To store the required answer
ans = 0
# Continue till number is lesser
# or not in same parity
while (getsum(ans) < n or
(getsum(ans) - n) & 1):
ans += 1
# Return the required answer
return ans
# Driver code
if __name__ == '__main__':
n = 9
print(countJumps(n))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find the number of jumps
// to reach X in the number line from zero
using System;
class GFG
{
// Utility function to calculate sum
// of numbers from 1 to x
static int getsum(int x)
{
return (x * (x + 1)) / 2;
}
// Function to find the number of jumps
// to reach X in the number line from zero
static int countJumps(int n)
{
// First make number positive
// Answer will be same either it is
// Positive or negative
n = Math.Abs(n);
// To store required answer
int ans = 0;
// Continue till number is lesser or not in same parity
while (getsum(ans) < n || ((getsum(ans) - n) & 1)>0)
ans++;
// Return the required answer
return ans;
}
// Driver code
static void Main()
{
int n = 9;
Console.WriteLine(countJumps(n));
}
}
// This code is contributed by mits
PHP
Javascript
输出:
5