PAT——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

下面两种方法都超时!!!

 1 package com.hone.basical;
 2 
 3 import java.util.Scanner;
 4 /**
 5  * 方法1
 6  * 原题目:https://www.patest.cn/contests/pat-b-practise/1047
 7  * @author Xia
 8  * 下面利用简单一点的方法解决,在读取数据的同时将数据分了,将学校相同的学生的成绩加起来
 9  * 时间复杂度为n
10  * 根据ID定位数据,建立以id为下标的数组
11  * 类似于:挖掘机技术哪家强
12  */
13 public class basicalLevel1047programTeamGame {
14 
15     public static void main(String[] args) {
16         Scanner in = new Scanner(System.in);
17         int n = in.nextInt();
18         int[] score = new int[1000];
19         int a;
20         int maxId = 0;            //标记得分最高的队伍
21         for (int i = 0; i < n; i++) {
22             String str = in.nextLine();
23             String[] strNum = str.split("\\-|\\s");
24             a = Integer.parseInt(strNum[0]);
25             score[a] += Integer.parseInt(strNum[2]);    //将a作为score数组的下标
26             if (score[a] > score[maxId]) {
27                 maxId = a;
28             }
29         }
30         System.out.println(maxId+" "+score[maxId]);
31     }
32 }

 

 1 package com.hone.basical;
 2 
 3 import java.util.Scanner;
 4 /**
 5  * 这种方法较为麻烦,还是推荐第一种的用teamNum做下标
 6  * 原题目:https://www.patest.cn/contests/pat-b-practise/1047
 7  * @author Xia
 8  */
 9 public class basicalLevel1047programTeamGame2 {
10 
11     public static void main(String[] args) {
12         Scanner in = new Scanner(System.in);
13         int n = Integer.parseInt(in.nextLine());
14         int[] teamNum = new int[n];
15         int[] personNum = new int[n];
16         int[] score = new int[n];
17         int[] sum = new int[n];
18         int sumMax = 0;
19         int k = 0;
20         for (int i = 0; i < n; i++) {
21             String str = in.nextLine();
22             String[] strNum = str.split("\\-|\\s");
23             teamNum[i] = Integer.parseInt(strNum[0]);
24             personNum[i] = Integer.parseInt(strNum[1]);
25             score[i] = Integer.parseInt(strNum[2]);
26         }
27         for (int i = 0; i < n; i++) {
28             for (int j = 0; j < n; j++) {
29                 if (teamNum[i]==teamNum[j]) {
30                     sum[i]+=score[j];
31                 }
32             }
33             if (sumMax<sum[i]) {
34                 sumMax = sum[i];
35                 k = i;
36             }
37         }
38         System.out.println(teamNum[k]+" "+sumMax);
39     }
40 }

 





posted @ 2017-12-07 10:18  SnailsCoffee  阅读(311)  评论(0编辑  收藏  举报