简体   繁体   English

连续负数算法

[英]Consecutive Negative Number Algorithm

For the life of me I am stuck on how to keep track of the maximum number of consecutive negative numbers. 在我的一生中,我一直坚持如何跟踪连续负数的最大数量。

I have a list of numbers that gets feed in to a class method during every iteration of a for loop. 我有一个数字列表,这些数字在for循环的每次迭代过程中都输入到类方法中。 These numbers can ether be positive or negative and there are no clear way of knowing what will follow what. 这些数字可以是正数,也可以是负数,并且没有明确的方法知道接下来会发生什么。 I need an algorithm so that it can calculate in real time (during loop execution) continuous the consecutive negative number. 我需要一种算法,以便它可以实时(在循环执行期间)连续计算连续的负数。 So at the end of the loop iteration the number stored in consecutiveNegative will be an integer showing the largest consecutive times a negative number was followed by another negative number. 因此,在该循环迭代存储在号码的末尾consecutiveNegative将示出最大的连续负的次数其次另一个负数的整数。

The below is what I've tried but it doesn't work... 以下是我尝试过的方法,但是不起作用...

class Temp
{
    public int consecutiveNegative = 0;
    private bool previousNegative = false;


    public void iterCall(int x)
    {
        if(x > 0)
        {
            if(previousNegative == true)
            {
                consecutiveNegative = 0;
            }
            previousNegative = false;

        }
        else if (x < 0)
        {
            if (previousNegative == false)
            {
                consecutiveNegative = consecutiveNegative + 1;
            }
            previousNegative = true;
        }
    }


}

Any pointers? 有指针吗?


Below is some code to get people started messing around. 下面是一些使人们开始混乱的代码。

Temp classTmp = new Temp();
List<int> nums = new List<int>();
nums.Add(1);
nums.Add(-1);
nums.Add(1);
nums.Add(-1);
nums.Add(-1);
nums.Add(-1);
nums.Add(1);
nums.Add(-1);
nums.Add(-1);
nums.Add(1);

for(int i = 0; i < nums.Count;i++)
{
    classTmp.iterCall(nums[i]);
}

classTmp.consecutiveNegative should be 3 after it finishes the loop. 完成循环后, classTmp.consecutiveNegative应该为3。

To my understanding, the code should be modified as follows. 据我了解,该代码应作如下修改。 There was no variable to keep track of the maximum sequence, and the negativity of the last input does not need to be stored. 没有变量可以跟踪最大序列,并且不需要存储最后一个输入的负数。 At any time, the desired result can be obtained by evaluating 随时可以通过评估获得期望的结果

Math.Max(ConsecutiveNegative,CurrentConsecutiveNegative)

as the sequence might not be terminated. 因为序列可能不会终止。

class Temp
{
    public int CurrentCosecutiveNegative = 0;
    public int ConsecutiveNegative = 0;

    public void iterCall(int x)
    {
        if(x >= 0)
        {
            ConsecutiveNegative
              = Math.Max(ConsecutiveNegative,CurrentConsecutiveNegative);
            CurrentConsecutiveNegative = 0;
        }
        else
        {
            CurrentConsecutiveNegative++;
        }
    }
}

You need a maxNegative , to keep track of your current longest-running consecutive negatives. 您需要一个maxNegative ,以跟踪当前运行时间最长的连续负数。 Also, your logic was faulty in your x < 0 case: the second time it was negative, it wouldn't increase the count. 同样,在x < 0情况下,您的逻辑有误:第二次为负数时,不会增加计数。

class Temp
    {
        public int consecutiveNegative = 0;
        public int maxNegative = 0;

        public void iterCall(int x)
        {
            if (x > 0)
            {
                consecutiveNegative = 0;
            }
            else if (x < 0)
            {
                consecutiveNegative += 1;
                if (maxNegative < consecutiveNegative)
                    maxNegative = consecutiveNegative;
            }
        }
    }

As @Codor showed, using this approach you don't need a boolean to keep track of the previous number, because consecutiveNegative will be reset automatically. 作为@Codor表明,使用这种方法,你不需要一个布尔跟踪对上号的,因为consecutiveNegative会自动复位。

You need 2 vars one for the largest negatives sequence and another for the current sequence length: 您需要2个变数,一个用于最大的负数序列,另一个用于当前序列长度:

class Temp
{
    public int consecutiveNegative = 0;
    public int curConsecutiveNegatives = 0;

    public void iterCall(int x)
    {
        if (x < 0)
            consecutiveNegative = Math.Max(consecutiveNegative, ++curConsecutiveNegatives);
        else
            curConsecutiveNegatives = 0;

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM