题目描述
老师想知道从某某同学当中,分数最高的是多少,现在请你编程模拟老师的询问。当然,老师有时候需要更新某位同学的成绩.
输入描述:
输入包括多组测试数据。每组输入第一行是两个正整数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
#include<stdio.h> #define N 3000 // 成绩 int score[N]; void Query(int, int); void Update(int, int); int main() { int n,m,s; int a,b; char op; printf("入正整数N和M,分别代表学生的数目和操作的数目 \n"); scanf("%d %d", &n, &m); printf("输入学生成绩,学生ID编号从1编到N \n"); for(int i=1;i<=n;i++){ scanf("%d",&s); score[i++]=s; } //执行询问或者更新操作 int j = 1; while(j <= m) { printf("请输入要执行的操作\n"); scanf("%c %d %d", &op, &a, &b); if(op == 'Q') { Query(a,b); j++; } if(op == 'U'){ Update(a,b); j++; } } return 0; } //查询操作,输出从 a到 b的最大字 void Query(int a, int b){ int max ; for(int i = a; i <= b; i++) { max= score[a]; if(score[i] > max) max = score[i]; } printf("最大值=%d\n", max); } //将第a个成绩改为 b void Update(int a, int b){ score[a] = b; }