链接:https://www.nowcoder.com/questionTerminal/3897c2bcc87943ed98d8e0b9e18c4666?answerType=1&f=discussion
来源:牛客网
输入包括多组测试数据。
每组输入第一行是两个正整数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。
import java.util.Scanner;
public class Main{
int n = 30000;
int m = 5000;
static int[] scores = new int[30000];
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while(in.hasNext()){//用来控制多组样例输入
String control = in.nextLine();
String controlArray[] = control.split(" ");
int scoreNum=Integer.parseInt(controlArray[0]);
int managNum=Integer.parseInt(controlArray[1]);
String score = in.nextLine();
String scoreArr[]= score.split(" ");
for(int i=0;i<scoreNum;i++){
int tem = Integer.parseInt(scoreArr[i]);
scores[i+1]=tem;
}
int num=1;
while(num<=managNum) {
//输入语句,执行
String m =in.nextLine();
String[] mArray = m.split(" ");
//char[] mCharArray = m.toCharArray();
int first = Integer.parseInt(mArray[1]);
int last = Integer.parseInt(mArray[2]);
if(mArray[0].equals("Q")) {//字符串比较大小,只能用equals,不能用==,因为不是同一个对象
queryMax(first,last);
}else if(mArray[0].equals("U")) {
update(first,last);
}
num++;
}
}
}
public static void queryMax(int first,int last) {
int max=0;
if(first<last){
for(int i=first;i<=last;i++) {
max=Math.max(max,scores[i]);
}
}
if(first==last){
max=scores[first];
}
if(first>last){
int tem = first;
first = last;
last=tem;
for(int i=first;i<=last;i++) {
max=Math.max(max,scores[i]);
}
}
System.out.println(max);
}
public static void update(int num,int score) {
scores[num]=score;
}
}
学到的东西:
1、 String control = in.nextLine();//用字符串接收一整行输入
String controlArray[] = control.split(" ");将字符串按指定tag拆分为字符串数组
int scoreNum=Integer.parseInt(controlArray[0]);将字符串转化为整型
int managNum=Integer.parseInt(controlArray[1]);
2、 if(mArray[0].equals("Q"))//字符串比较大小,只能用equals
3、用
while(in.nextLine()){
所有需要输出处理的内容
}
控制多组样例的输入