题目介绍:现在输入一组数据,写入学生的考试分数。已知学生数为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 }
结果: