图解算法——查询系统,最高分是多少

1、题目描述

老师想知道从某某同学当中,分数最高的是多少,现在请你编程模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

2、示例

输入描述:

输入包括多组测试数据。
每组输入第一行是两个正整数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。

输出描述:

对于每一次询问操作,在一行里面输出最高成绩。

示例1:

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 5 1

输出:

5
6
5
9

NOTE 注意:

1>需要说明的是,这里输出看起来虽然是四个一起换行输出的,但是其实不是的,而是要求在查询后,立即换行输出。

2>注意到最后一个查询语句“Q 5 1”,A=5,B=1,题中并没有说明 A > B时是否为错,所以这里作为正确使用。

3、解题思路

分别获取到人数,操作数,以及分数数组。

在操作数内循环操作,当读到的首个字符为 ‘Q’ 时,进行查询操作;当读到的首字符为 'U' 时,为更新操作。

这里需要注意 的是,读到字符的语句没有 nextChar() 方法,我们只能先利用 next() 读到字符串,然后再利用String类中的 charAt(int index) 法获取到首字符。

还有一个注意点是:Q 或 U 操作时,注意下标是从 1 开始的,要和数组的下标对上的话,需要减 1 。

 代码如下:

先粘上我第一次写的半错误代码,看看大家能发现哪里有错吗?嘿嘿嘿.......

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int[] count = new int[N];
        int maxCount = 0;
        for(int i = 0; i<N; i++){
            count[i] = sc.nextInt();
        }
        for(int j = 0; j<M; j++){
            char c = sc.next().charAt(0);
            int A = sc.nextInt();
            int B = sc.nextInt();
            
            if(c == 'Q'){
                maxCount = query(count,A,B);
            }else if(c == 'U'){
                count[A] = B;
            }
            System.out.println(maxCount);
        }
    }
    
    public static int query(int[] count,int A,int B){
        int max = count[A];
        if(A>B){
            return -1;
        }
        if(A==B){
            return count[A];
        }
        for(int i = A; i<=B; i++){
            max = count[i] > max ? count[i] : max;
        }
        return max;
    }
}

上述代码可以将示例中(除了最后一个Q操作)的操作顺利执行。

但是加上最后一个Q操作后,就会出错,看明白了吗?

正确代码如下:

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = 0;
        int M = 0;
        while(sc.hasNext()){
            N = sc.nextInt();
            M = sc.nextInt();
            int[] count = new int[N];
            int maxCount = 0;
            for(int i = 0; sc.hasNext() && i<N; i++){
                count[i] = sc.nextInt();
            }
            for(int j = 0; j<M; j++){
                char c = sc.next().charAt(0);
                int A = sc.nextInt();
                int B = sc.nextInt();

                if(c == 'Q'){
                    maxCount = query(count,A-1,B-1);
                    System.out.println(maxCount);
                }else if(c == 'U'){
                    count[A-1] = B;
                }
            }
        }
        
        sc.close();
    }
    
    public static int query(int[] count,int A,int B){
        int max = count[A];
        //注意这里调换大小值
        if(A>B){
            int a = A;
            A = B;
            B = a;
        }
        if(A==B){
            return count[A];
        }
        for(int i = A; i<=B; i++){
            max = count[i] > max ? count[i] : max;
        }
        return max;
    }
}

Over..........

原文地址:https://www.cnblogs.com/gjmhome/p/15082985.html