简体   繁体   English

如何找到连续自然继承者的最长序列?

[英]How to find the longest sequence of consecutive natural successors?

Two consecutive integers are natural successors if the second is the successor of the first in the sequence of natural numbers (1 and 2 are natural successors). 如果第二个连续整数是自然数序列中第一个的后继,则两个连续整数为自然后继(1和2是自然后继)。 Write a program that reads a number N followed by N integers, and then prints the length of the longest sequence of consecutive natural successors. 编写一个程序,该程序读取一个N后跟N个整数的数字,然后打印连续自然继承者中最长序列的长度。

import java.util.Scanner;

public class objects {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();
        int nmbr[] = new int[x];
        for(int f=0;f<nmbr.length;f++)
            nmbr[f]=scan.nextInt();
        int counter = 0;
        int max = 0;
        for (int i = 0; i < nmbr.length - 1; i++) {
            if (nmbr[i] == (nmbr[i + 1]-1))
                counter++;
            else
                counter = 0;
            if (counter > max)
                max = (counter+1);
        }
        System.out.println(max);
    }
}

Why is my code still printing the counter without adding one? 为什么我的代码仍不打印计数器就打印计数器? I am not finding the mistake sample run: 我找不到错误的示例运行:

     7 2 3 5 6 7 9 10 
     2

it is printing 2 instead of 3. 它正在打印2而不是3。

When you see the first number out of sequence, you should reset counter to 1, not 0 - as it's the start of a sequence with length at least 1. You then need to also change the code which changes max : 当您看到第一个数字按顺序排列时,应将counter重置为1,而不是0-因为它是长度至少为 1的序列的开始。您还需要更改可更改max的代码:

if (counter > max) {
    counter = max;
}

After all, you just want max to be the maximum value of counter . 毕竟,您只希望maxcounter的最大值。

(I would strongly recommend using braces for every if statement, by the way - it's easier to avoid mistakes that way.) (顺带一提,我强烈建议为每个 if语句使用花括号-这样更容易避免错误。)

 import java.util.Scanner;

 public class objects {
     public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();
        int nmbr[] = new int[x];
        for(int f=0;f<nmbr.length;f++) {
            nmbr[f]=scan.nextInt();
        }
        int counter = 0;
        int max = 0;
        for (int i = 0; i < nmbr.length - 1; i++) {
            if (nmbr[i] == (nmbr[i + 1]-1)) {
                counter++;
            } else {
                if (counter > max)
                    max = (counter+1);
                counter = 0;
            }
        }
        System.out.println(max);
    }
}

Just a minor error in your logic there, you were updating max every single time the counter was greater than max, so the next time if the sequence comes to an end, it actually fails to register as the longest sequence. 那里的逻辑只是一个小错误,您每次在计数器大于max时都更新max ,因此下一次如果序列结束,则实际上无法注册为最长序列。 Just reorder it as I have done and it works. 只需重新排序即可,它可以正常工作。

you don't take into account the first number, in this case 5 您不考虑第一个数字,在这种情况下为5

when i == 2 its the start of the sequence, however, nmbr[1] is 3 therfore counter will stil be 0 i == 2它是序列的开始,但是, nmbr[1]为3,因此计数器nmbr[1] 0

start the counter to be 1 (this is logic, since the smallest sequence is 1) you need to change the if to be: 将计数器设为1(这是逻辑,因为最小的序列为1),因此您需要将if更改为:

import java.util.Scanner;

public class objects {
     public static void main(String[] args) {

         Scanner scan = new Scanner(System.in);
         int x = scan.nextInt();
         int nmbr[] = new int[x];
         for(int f=0;f<nmbr.length;f++)
             nmbr[f]=scan.nextInt();
         int counter = 1;
         int max = 1;
         for (int i = 0; i < nmbr.length - 1; i++) {
             if (nmbr[i] == (nmbr[i + 1]-1))
                 counter++;
             else
                 counter = 1;
             if (counter > max)
                 max = counter;
         }
         System.out.println(max);
    }
} 

Your maximum condition is incorrect: 您的最大条件不正确:

if (counter > max)
    max = (counter+1);

You compare max with counter and assign counter+1 . 您将max与counter进行比较,并分配counter+1 This way when max is equal to 2 it can't be updated with 2+1. 这样,当max等于2时,无法使用2 + 1进行更新。

Probably you should use: 可能您应该使用:

if (counter + 1 > max)
    max = (counter+1);    

PS: Your question doesn't show research effort. PS:您的问题并未显示出研究成果。 You could use debug output to understand what is going on with your program. 您可以使用调试输出来了解程序的运行状况。 Simplistic example: 简单的例子:

for (int i = 0; i < nmbr.length - 1; i++) {
    if (nmbr[i] == (nmbr[i + 1]-1))
        counter++;
    else
        counter = 0;
    System.out.println("Current value: "+counter);
    if (counter > max) {
        max = (counter+1);
        System.out.println("New max value: "+max);
    }
}

Start your conter from 1. because if you start from 0 then it means you are not counting the 1st value. 从1开始转换,因为如果从0开始,则意味着您没有在计算第一个值。

else
    counter = 1;

And Change the dont increment your counter when assigning to max. 并在分配给最大值时更改不要增加计数器。

max =counter;

This was an easy one and already pointed out in previous answers you are wrongly setting the value of counter in your code. 这是一个简单的方法,在先前的答案中已经指出,您在代码中错误地设置了counter的值。

Run your code through a debugger to keep a track of variable values of what you are expecting from your program versus what you are actually getting. 通过调试器运行代码,以跟踪变量对程序期望值和实际值的期望值。

Stepping through each line of code can help you understand your mistakes better. 逐步执行每一行代码可以帮助您更好地理解错误。

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

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