题目描述
输入长度为 n 的一个正整数序列,要求输出序列中最长连号的长度。
连号指在序列中,从小到大的连续自然数。
输入格式
第一行,一个整数 n。
第二行,n 个整数 ai,之间用空格隔开。
输出格式
一个数,最长连号的个数。
输入输出样例
输入 #1
10 1 5 6 2 3 4 5 6 8 9
输出 #1
5
说明/提示
数据规模与约定
对于 100% 的数据,保证 1≤n≤104,1≤ai≤109。
代码如下:
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int a = scanner.nextInt();
int[] arr=new int [30000];
int s=1,max=0;
for(int i=1;i<=a;i++){
arr[i]=scanner.nextInt();
if(i>=2){
if(arr[i]==arr[i-1]+1){
s++;
}else s=1;
if(s>max) max=s;
}
}
System.out.println(max);
}
}