📜  在数字行中找到从零到X的跳数

📅  最后修改于: 2021-04-26 06:15:35             🧑  作者: Mango

给定一个整数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;
  
// Utitlity 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 
{ 
      
// Utitlity 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
  
# Utitlity 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
{
      
// Utitlity 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


输出:
5