1. 程式人生 > >最高分是多少(華為機試)

最高分是多少(華為機試)

老師想知道從某某同學當中,分數最高的是多少,現在請你程式設計模擬老師的詢問。當然,老師有時候需要更新某位同學的成績. 
輸入描述:
輸入包括多組測試資料。
每組輸入第一行是兩個正整數N和M(0 < N <= 30000,0 < M < 5000),分別代表學生的數目和操作的數目。
學生ID編號從1編到N。
第二行包含N個整數,代表這N個學生的初始成績,其中第i個數代表ID為i的學生的成績
接下來又M行,每一行有一個字元C(只取‘Q’或‘U’),和兩個正整數A,B,當C為'Q'的時候, 表示這是一條詢問操作,他詢問ID從A到B(包括A,B)的學生當中,成績最高的是多少
當C為‘U’的時候,表示這是一條更新操作,要求把ID為A的學生的成績更改為B。


輸出描述:
對於每一次詢問操作,在一行裡面輸出最高成績.

輸入例子:
5 7
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 4 5
U 2 9
Q 1 5

輸出例子:
5
6
5
9
import java.util.ArrayList;
import java.util.Scanner;


public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n, m;
do {
n = sc.nextInt();
m = sc.nextInt();
ArrayList<Integer> array = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
array.add(sc.nextInt());
}
char a;
int b, c, num = 0;
while (num < m) {
a = sc.next().charAt(0);
b = sc.nextInt();
c = sc.nextInt();
if ('Q' == a) {
int start, end;
if (b > c) {
start = c - 1;
end = b - 1;
} else {
start = b - 1;
end = c - 1;
}
int max = array.get(start);
for (int j = start; j <= end; j++) {
if (max <= array.get(j)) {
max = array.get(j);
}
}
System.out.println(max);
}
if ('U' == a) {
int index1 = b - 1;
int newValue = c;
array.set(index1, newValue);
}
sc.nextLine();
num++;
}
} while (sc.hasNext());
sc.close();
}
}
我的幾個錯誤:
1:do---while;
2:丟擲異常