題目:遞增序列
問題描述
在數列 a[1], a[2], …, a[n] 中,如果 a[i] < a[i+1] < a[i+2] < … < a[j],則稱 a[i] 至 a[j] 爲一段遞增序列,長度爲 j-i+1。
給定一個數列,請問數列中最長的遞增序列有多長。
輸入格式
輸入的第一行包含一個整數 n。
第二行包含 n 個整數 a[1], a[2], …, a[n],相鄰的整數間用空格分隔,表示給定的數列。
輸出格式
輸出一行包含一個整數,表示答案。
樣例輸入
7
5 2 4 1 3 7 2
樣例輸出
3
要點
題目給定一個數列,他可能前面一部分是遞增序列,然後斷掉了,然後後面又有一個遞增序列,你得比較兩端序列的長度
代碼
import java.util.Scanner;
public class 遞增序列 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
sc.close();
int temp = array[0];
int count = 1;
int max = 0;
for (int i = 1; i < n; i++) {
if (temp < array[i]) {
temp = array[i];
count++;
} else { //重新開始一段遞增序列
temp = array[i];
max = Math.max(count, max);
count = 1;
}
}
System.out.println(max);
}
}