华为真题 最高分是多少

import java.util.Scanner;

public class TopOfScore {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			int m = sc.nextInt();
			int[] score = new int[n];
			String operation= null;
			int a = 0;
			int b = 0;
			
			for (int i = 0; i < n; i++) {
				score[i] = sc.nextInt();
			}
			sc.nextLine();
			for (int j = 0; j < m; j++) {
				String[] strs = sc.nextLine().split(" ");
				operation = strs[0];
				a = Integer.parseInt(strs[1])-1;
				b = Integer.parseInt(strs[2])-1;
				int max = 0;
				//Q操作和U操作是分开的不能一起判断a 和b的关系因为以下对换影响了U操作所以导致错误。
				/*if(a>b) {
					a =a^b;
					b =a^b;
					a = a^b;
				}*/
				if(operation.equals("Q")) {
					for(int k = Math.min(a, b);k<=Math.max(a, b);k++) {
						max = Math.max(max, score[k]);
					}
					System.out.println(max);
				}
				if(operation.equals("U")) {
					score[a] =b+1;
				}
			}
		}
	}

}

有两点需要注意。

1.query查询的时候有可能a 和b的大小关系会导致程序的错误。因为数组查询的时候一定是a<b的。

2.对换a 和b是针对query操作的如果在步骤之中没有区分query操作和update操作会直接导致运算结果的错误。

思考:

用Scanner类是一并输入保存为字符串类型然后利用split()函数进行分割是不是比一个一个输入更加高效率呢?

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值