美文网首页
PAT-B 1047. 编程团体赛(20)

PAT-B 1047. 编程团体赛(20)

作者: Rush的博客 | 来源:发表于2016-10-09 16:20 被阅读333次

传送门

https://www.patest.cn/contests/pat-b-practise/1047

题目

编程团体赛的规则为:每个参赛队由若干队员组成;所有队员独立比赛;参赛队的成绩为所有队员的成绩和;成绩最高的队获胜。
现给定所有队员的比赛成绩,请你编写程序找出冠军队。
输入格式:
输入第一行给出一个正整数N(<=10000),即所有参赛队员总数。随后N行,每行给出一位队员的成绩,格式为:“队伍编号-队员编号 成绩”,其中“队伍编号”为1到1000的正整数,“队员编号”为1到10的正整数,“成绩”为0到100的整数。
输出格式:
在一行中输出冠军队的编号和总成绩,其间以一个空格分隔。注意:题目保证冠军队是唯一的。
输入样例:
6
3-10 99
11-5 87
102-1 0
102-3 100
11-9 89
3-2 61
输出样例:
11 176

分析

先建个表存放各队伍的成绩,下标为队伍编号,题目输入的队员编号其实是没有用的,把每个成绩都加到对应的队伍上,然后输出最大值和最大值的队伍就行。

源代码

//C/C++实现
#include <iostream>

using namespace std;

int queue[1001];

int main(){
    int n;
    scanf("%d", &n);
    int max = 0, max_index = -1;
    for(int i = 0; i < n; ++i){
        int queueNo, memberNo, grade;
        scanf("%d-%d %d", &queueNo, &memberNo, &grade);
        // memberNo没卵用的,就来客串一下
         queue[queueNo] += grade;
         if(queue[queueNo] > max){
            max = queue[queueNo];
            max_index = queueNo;
         }
    }
    printf("%d %d\n", max_index, max);
    return 0;
}
//Java实现
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String s = bufferedReader.readLine();
        int n = 0;
        try{
            n = Integer.valueOf(s);
        }catch (Exception e){
            System.exit(0);
        }
        if(n >0 && n <= 10000){
            int[] array = new int[1001];
            int max = 0,max_index = 0,array_max_index = 0;
            int team = 0,no = 0,grade = 0;
            for(int i=0;i<n;i++){
                String ss = bufferedReader.readLine();
                String[] arraySs = ss.split(" ");
                String[] sp = arraySs[0].split("-");
                try {
                    team = Integer.valueOf(sp[0]);
                    no = Integer.valueOf(sp[1]);
                    grade = Integer.valueOf(arraySs[1]);
                }catch (Exception e){
                    System.exit(0);
                }
                if(team < 1 || team > 1000 || no < 1 || no > 10 || grade < 0 || grade > 100 ){
                    System.exit(0);
                }
                array[team] += grade;
                if(team > array_max_index){
                    array_max_index = team;
                }
            }
            for(int k=0;k<array_max_index+1;k++){
                if(array[k]>max) {
                    max = array[k];
                    max_index = k;
                }
            }
            System.out.println(max_index+" "+max);
        }
    }
}

相关文章

网友评论

      本文标题:PAT-B 1047. 编程团体赛(20)

      本文链接:https://www.haomeiwen.com/subject/fdapyttx.html