【华为OD-E卷】Java编程实战解析:MVP争夺战——满分攻略与思路解析

MVP争夺战

题目描述

在MVP争夺战中,有N名球员进行比赛,最终选出MVP(最有价值球员)。比赛规则如下:

  1. 初始得分:每名球员的初始得分为0。
  2. 比赛轮次:共有M轮比赛,每轮比赛由两个球员进行对决。
  3. 对决规则:每轮比赛,两名球员对决,得分高的球员赢得该轮比赛,并将对手的分数加到自己的分数上。如果两名球员得分相同,则本轮比赛算作平局,双方分数不变。
  4. MVP判定:所有比赛结束后,得分最高的球员即为MVP。如果有多名球员得分相同,则输出编号最小的球员。

给定球员数量N,比赛轮次M,以及每轮比赛的对决信息,计算并输出MVP的编号。

输入描述

  • 第一行输入两个整数N和M,分别表示球员数量和比赛轮次。
  • 接下来M行,每行输入两个整数A和B,表示第i轮比赛由编号为A的球员和编号为B的球员对决。
  • 输出描述

  • 输出一个整数,表示MVP的编号。
  • 示例

    输入

    5 4
    1 2
    3 4
    1 3
    2 5

    输出

    1

    思路

    1. 数据结构:使用一个数组来存储每名球员的得分。
    2. 比赛模拟:遍历每轮比赛,根据对决规则更新球员得分。
    3. MVP判定:遍历所有球员得分,找到得分最高的球员,如果有多个最高分,则选择编号最小的球员。

    Java解析

    import java.util.Scanner;

    public class MVPPlayer {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);

            int N = scanner.nextInt();
            int M = scanner.nextInt();

            int[] scores = new int[N + 1]; // 球员编号从1开始,数组下标从0开始,故数组大小为N+1

            for (int i = 0; i < M; i++) {
                int A = scanner.nextInt();
                int B = scanner.nextInt();

                if (scores[A] > scores[B]) {
                    scores[A] += scores[B];
                    scores[B] = 0;
                } else if (scores[A] < scores[B]) {
                    scores[B] += scores[A];
                    scores[A] = 0;
                } // 平局时不做处理
            }

            int maxScore = 0;
            int mvp = 0;

            for (int i = 1; i <= N; i++) {
                if (scores[i] > maxScore || (scores[i] == maxScore && i < mvp)) {
                    maxScore = scores[i];
                    mvp = i;
                }
            }

            System.out.println(mvp);
        }
    }

    C++解析

    #include <iostream>
    #include <vector>
    #include <algorithm>

    using namespace std;

    int main() {
        int N, M;
        cin >> N >> M;

        vector<int> scores(N + 1, 0); // 球员编号从1开始,数组下标从0开始,故数组大小为N+1

        for (int i = 0; i < M; i++) {
            int A, B;
            cin >> A >> B;

            if (scores[A] > scores[B]) {
                scores[A] += scores[B];
                scores[B] = 0;
            } else if (scores[A] < scores[B]) {
                scores[B] += scores[A];
                scores[A] = 0;
            } // 平局时不做处理
        }

        int maxScore = 0;
        int mvp = 0;

        for (int i = 1; i <= N; i++) {
            if (scores[i] > maxScore || (scores[i] == maxScore && i < mvp)) {
                maxScore = scores[i];
                mvp = i;
            }
        }

        cout << mvp << endl;

        return 0;
    }

    Python解析

    def main():
        import sys
        input = sys.stdin.read
        data = input().split()
        
        index = 0
        N = int(data[index]); index += 1
        M = int(data[index]); index += 1
        
        scores = [0] * (N + 1) # 球员编号从1开始,数组下标从0开始,故数组大小为N+1
        
        for _ in range(M):
            A = int(data[index]); index += 1
            B = int(data[index]); index += 1
            
            if scores[A] > scores[B]:
                scores[A] += scores[B]
                scores[B] = 0
            elif scores[A] < scores[B]:
                scores[B] += scores[A]
                scores[A] = 0
        
        max_score = 0
        mvp = 0
        
        for i in range(1, N + 1):
            if scores[i] > max_score or (scores[i] == max_score and i < mvp):
                max_score = scores[i]
                mvp = i
        
        print(mvp)

    if __name__ == "__main__":
        main()

    作者:执着的小火车

    物联沃分享整理
    物联沃-IOTWORD物联网 » 【华为OD-E卷】Java编程实战解析:MVP争夺战——满分攻略与思路解析

    发表回复