📜  计算图形穿过 X 轴的次数

📅  最后修改于: 2021-10-23 08:52:05             🧑  作者: Mango

给定一个大小为 N 的整数数组arr[] ,任务是找出图形穿过 X 轴的次数,其中正数表示超出其当前位置该值,负数表示下降该值价值。最初,当前位置在原点。

例子:

方法:遍历数组,将前一级别和当前级别的值保存到两个变量中。最初,两个水平都为零。按数组中给定的值增加/减少级别,并在以下两种情况下增加计数。

  • 如果前一级别小于零且当前级别大于或等于零。
  • 如果前一级别大于零且当前级别小于或等于零。

下面是上述方法的实现。

C++
// C++ implementation to count the
// number of times the graph
// crosses the x-axis.
 
#include 
using namespace std;
 
// Function to to count the
// number of times the graph
// crosses the x-axis.
int times(int steps[], int n)
{
 
    int current_level = 0;
    int previous_level = 0;
    int count = 0;
 
    // Iterate over the steps array
    for (int i = 0; i < n; i++) {
 
        // Update the previous level and
        // current level by value given
        // in the steps array
        previous_level = current_level;
        current_level = current_level
                        + steps[i];
 
        // Condition to check that the
        // graph crosses the origin.
        if ((previous_level < 0
             && current_level >= 0)
            || (previous_level > 0
                && current_level <= 0)) {
            count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
{
    int steps[12] = { 1, -1, 0, 0, 1, 1, -3, 2 };
    int n = sizeof(steps) / sizeof(int);
 
    cout << times(steps, n);
    return 0;
}


Java
// Java implementation to count the
// number of times the graph
// crosses the x-axis.
class GFG
{
     
    // Function to to count the
    // number of times the graph
    // crosses the x-axis.
    static int times(int []steps, int n)
    {
        int current_level = 0;
        int previous_level = 0;
        int count = 0;
     
        // Iterate over the steps array
        for (int i = 0; i < n; i++)
        {
     
            // Update the previous level and
            // current level by value given
            // in the steps array
            previous_level = current_level;
            current_level = current_level + steps[i];
     
            // Condition to check that the
            // graph crosses the origin.
            if ((previous_level < 0 &&
                current_level >= 0)
                || (previous_level > 0
                && current_level <= 0))
            {
                count++;
            }
        }
        return count;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int steps[] = { 1, -1, 0, 0, 1, 1, -3, 2 };
        int n = steps.length;
     
        System.out.println(times(steps, n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation to count the
# number of times the graph
# crosses the x-axis.
 
# Function to to count the
# number of times the graph
# crosses the x-axis.
def times(steps, n):
 
    current_level = 0
    previous_level = 0
    count = 0
 
    # Iterate over the steps array
    for i in range(n):
 
        # Update the previous level and
        # current level by value given
        #in the steps array
        previous_level = current_level
        current_level = current_level+ steps[i]
 
        # Condition to check that the
        # graph crosses the origin.
        if ((previous_level < 0
            and current_level >= 0)
            or (previous_level > 0
                and current_level <= 0)):
            count += 1
 
    return count
 
# Driver Code
steps = [1, -1, 0, 0, 1, 1, -3, 2]
n = len(steps)
 
print(times(steps, n))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to count the
// number of times the graph
// crosses the x-axis.
using System;
 
class GFG
{
     
    // Function to to count the
    // number of times the graph
    // crosses the x-axis.
    static int times(int []steps, int n)
    {
        int current_level = 0;
        int previous_level = 0;
        int count = 0;
     
        // Iterate over the steps array
        for (int i = 0; i < n; i++)
        {
     
            // Update the previous level and
            // current level by value given
            // in the steps array
            previous_level = current_level;
            current_level = current_level + steps[i];
     
            // Condition to check that the
            // graph crosses the origin.
            if ((previous_level < 0 &&
                current_level >= 0)
                || (previous_level > 0
                && current_level <= 0))
            {
                count++;
            }
        }
        return count;
    }
     
    // Driver Code
    public static void Main ()
    {
        int []steps = { 1, -1, 0, 0, 1, 1, -3, 2 };
        int n = steps.Length;
     
        Console.WriteLine(times(steps, n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程