题目
Description
已知一个已经从小到大排列好的数组,说这个数组中的一个平台
(Plateau),就是连续的一串值相同的元素,并且这一串元素不能再延
伸。例如,在 1,2,2,3,3,3,4,5,5,6 中 1,2.2,3.3.3,4,
5.5,6 都是平台。试编写一个程序,接收一个数组,把这个数组中最长的
平台找出来。在上面的例子中 3.3.3 是该数组中最长的平台。(注:为了
方便,测试用例的数组最大长度都不超过 50,数组中的元素全部为整数范
围为[-2^31,2^31-1])。
Input
多组测试数据,处理到文件结尾。每组数据首先由一个数组的长度 L,随后
跟随 L 个该数组数据组成。
Output
对于每个测试用例输出它的最长平台
Sample Input
10
1 2 2 3 3 3 4 5 5 6
11
1 1 1 1 1 1 2 2 2 2 2
5
1 2 3 4 5
Sample Output
3
6
1
代码块
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cn = new Scanner(System.in);
while(cn.hasNext()){
int n = cn.nextInt();
int[] a = new int[n];
for(int i =0;i<n;i++){
a[i] = cn.nextInt();
}
int max = 0;
for(int i =0;i<n;i++){
int counts =0;
for(int j =i+1;j<n;j++){//进行循环判断是否重复
if(a[i]==a[j]){
counts ++;//并计算重复的度数
i =j;//同时返回到检查的j值,之前的i值就可以省略检查
}else break;
}
if(max<counts) max = counts;//更新最大的度数
}
System.out.println(max+1);//输出度数并加上他的本身
}
}
}