您的位置:首页 > 编程语言

PAT1047编程团体赛(20)

2016-03-08 11:14 253 查看
送分题。。。

编程团体赛的规则为:每个参赛队由若干队员组成;所有队员独立比赛;参赛队的成绩为所有队员的成绩和;成绩最高的队获胜。

现给定所有队员的比赛成绩,请你编写程序找出冠军队。

输入格式:

输入第一行给出一个正整数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

struct codePlayer
{
int teamID;
int playerID;
int score;
};
void findMax(codePlayer co[],int n){
int teamIDs[1001] = {0};
int maxScore=0,maxID;
for (int i = 1; i < n; ++i){
teamIDs[co[i].teamID] += co[i].score;
}

for (int i = 1; i < 1001;++i){
if (maxScore < teamIDs[i]){
maxScore = teamIDs[i];
maxID = i;
}
}
cout << maxID << " " << maxScore << endl;
}
codePlayer funPAT(string test){
int num[4];
stringstream ss;
int k = 0;
int len = test.length();
string tmp;
for (int i = 0; i <= len; ++i){
if (test[i] == '-' || i == len || test[i] == ' '){
ss << tmp;
ss >> num[k++];
ss.clear();
tmp.clear();
}
else if (i<len)tmp.push_back(test[i]);
}

codePlayer co;
co.score = num[2];
co.playerID = num[1];
co.teamID = num[0];
return co;
}
void PAT1047(){
int N = 0;
string tmp;
vector<string> sent;
cin >> N;
++N;
while (N--){
getline(cin, tmp);
sent.push_back(tmp);
tmp.clear();
}
int lenV = sent.size();
codePlayer*cox = new codePlayer[lenV];
for (int i = 1; i < lenV; ++i){
cox[i] = funPAT(sent[i]);
}
findMax(cox,lenV);
}


上面方法其实是用来练手的,因为它涉及到了比较多的东西,下面是真正的解题:

void PAT1047(){
int t, num, score;
int M,maxID;
cin >> M;
int team[1001] = { 0 };
for (int i = 1; i <= M; ++i){
scanf("%d-%d %d", &t, &num, &score);
team[t] += score;
}
int maxScore = 0;
for (int i = 0; i < 1001; ++i){
if (maxScore < team[i]){ maxScore = team[i]; maxID = i; }
}
cout << maxID << " " << maxScore << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: