发现连续编号的最长序列 [英] finding longest sequence of consecutive numbers

查看:142
本文介绍了发现连续编号的最长序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题H(最长的天然接班人):

连续两个整数是天然的接班人,如果第二个是第一个自然数(1和2是自然的继任者)的序列中的继任者。编写一个程序,读取数N其次是N个整数,然后打印连续天然接班人的最长序列的长度。

例如:

输入7 2 3 5 6 7 8 9 10输出3这是我的code,到目前为止,我不知道为什么它不能正常工作

 进口java.util.Scanner中;公共类形成机制{    公共静态无效的主要(字串[] args){
        扫描程序扫描=新的扫描仪(System.in);
        INT X = scan.nextInt();
        INT []数组=新INT [X];
        的for(int i = 0; I< array.length,我++){
            数组[我] = scan.nextInt();
        }
        的System.out.println(阵列(阵列));    }    公共静态int数组(INT []数组){
        诠释计数= 0,温度= 0;
        的for(int i = 0; I< array.length,我++){
            计数= 0;
            对于(INT J = I,K = I + 1; J< array.length - 1; J ++,K ++){
                如果(Math.abs(阵列[J] - 阵[K])== 1){
                    算上++;
                }其他{
                    如果(温度< =计数){
                        TEMP =计数;
                    }
                    打破;
                }
            }
        }
        返回温度+ 1;
    }}


解决方案

为什么两个循环?怎么样

 公共静态int数组(最终诠释[]数组){
    INT lastNo = -100;
    INT maxConsecutiveNumbers = 0;
    INT currentConsecutiveNumbers = 0;    的for(int i = 0; I< array.length,我++){
        如果(阵列[我] == lastNo + 1){
            currentConsecutiveNumbers ++;
            maxConsecutiveNumbers = Math.max(maxConsecutiveNumbers,
                    currentConsecutiveNumbers);
        }其他{
            currentConsecutiveNumbers = 1;
        }
        lastNo =阵列[我]
    }
    返回Math.max(maxConsecutiveNumbers,currentConsecutiveNumbers);
}

Problem H (Longest 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). 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.

Example:

Input 7 2 3 5 6 7 9 10 Output 3 this is my code so far and i have no idea why it does not work

import java.util.Scanner;

public class Conse {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();
        int[] array = new int[x];
        for (int i = 0; i < array.length; i++) {
            array[i] = scan.nextInt();
        }
        System.out.println(array(array));

    }

    public static int array(int[] array) {
        int count = 0, temp = 0;
        for (int i = 0; i < array.length; i++) {
            count = 0;
            for (int j = i, k = i + 1; j < array.length - 1; j++, k++) {
                if (Math.abs(array[j] - array[k]) == 1) {
                    count++;
                } else {
                    if (temp <= count) {
                        temp = count;
                    }
                    break;
                }
            }
        }
        return temp + 1;
    }

}

解决方案

Why two loops? What about

public static int array(final int[] array) {
    int lastNo = -100;
    int maxConsecutiveNumbers = 0;
    int currentConsecutiveNumbers = 0;

    for (int i = 0; i < array.length; i++) {
        if (array[i] == lastNo + 1) {
            currentConsecutiveNumbers++;
            maxConsecutiveNumbers = Math.max(maxConsecutiveNumbers,
                    currentConsecutiveNumbers);
        } else {
            currentConsecutiveNumbers = 1;
        }
        lastNo = array[i];
    }
    return Math.max(maxConsecutiveNumbers, currentConsecutiveNumbers);
}

这篇关于发现连续编号的最长序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆