題目介紹:現在輸入一組數據,寫入學生的考試分數。已知學生數為N,學生編號為1到N,且0<N<=30000,每個學生都有一個分數;操作數為M且0<M<5000。輸入第一行為N M,接下來是1行N列數據代表學生的初試分數,接下來是M行操作數據。已知操作有兩種,分為Q和U。一次操作的格式為 C A B,當 ...
題目介紹:現在輸入一組數據,寫入學生的考試分數。已知學生數為N,學生編號為1到N,且0<N<=30000,每個學生都有一個分數;操作數為M且0<M<5000。輸入第一行為N M,接下來是1行N列數據代表學生的初試分數,接下來是M行操作數據。已知操作有兩種,分為Q和U。一次操作的格式為 C A B,當C=Q時輸出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
分析:一開始是設想char一個m行3列的數組,但是考慮到ID和分數都可能不是個位數因此還是分別設置好了。查詢指令中A不一定比B小這一點也要考慮到。
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int m, n; 6 int result = 0; 7 while (cin >> n >> m) 8 { 9 int *score = new int[n]; 10 for (int i = 0; i < n; i++) 11 { 12 cin >> score[i]; 13 } 14 char *cha = new char[m]; 15 int *one = new int[m]; 16 int *two = new int[m]; 17 for (int i = 0; i < m; i++) 18 { 19 cin >> cha[i] >> one[i] >> two[i]; 20 } 21 for (int i = 0; i < m; i++) 22 { 23 if (cha[i] == 'Q') 24 { 25 if (two[i] > one[i]) 26 { 27 for (int j = one[i] - 1; j <= two[i] - 1; j++) 28 { 29 if (score[j] >= result) 30 { 31 result = score[j]; 32 } 33 } 34 } 35 else { 36 for (int j = two[i] - 1; j <= one[i] - 1; j++) 37 { 38 if (score[j] >= result) 39 { 40 result = score[j]; 41 } 42 } 43 } 44 cout << result << endl; 45 result = 0; 46 } 47 if (cha[i] == 'U') 48 { 49 score[one[i] - 1] = two[i]; 50 } 51 } 52 } 53 }
結果: