1. 程式人生 > >PAT 乙級 1047 編程團體賽

PAT 乙級 1047 編程團體賽

TE getch name inner void spa friend rst delete

技術分享圖片

輸入樣例:
6
3-10 99
11-5 87
102-1 0
102-3 100
11-9 89
3-2 61
輸出樣例:
11 176

思路是建立一張表,以隊伍編號為關鍵字,以隊伍總成績為關鍵字對應的值。統計時直接查表。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

struct TeamMember
{
    int idOfTeam,idOfMember,score;
    friend istream& operator>>(istream& str,TeamMember& m)
    {
        cin>>m.idOfTeam;
        getchar();
        cin>>m.idOfMember;
        getchar();
        cin>>m.score;
    }
};


void countWinner(TeamMember* p,int n);

int main()
{
    int n;
    cin>>n;
    TeamMember* p=new TeamMember[n];
    for(int i=0;i<n;i++)
        cin>>p[i];
    countWinner(p,n);
    delete p;
    return 0;
}

void countWinner(TeamMember* p,int n)
{
    map<int,int> m;
    map<int,int>::iterator itor;
    for(int i=0;i<n;i++)
    {
        itor=m.find(p[i].idOfTeam);
        if(itor==m.end())
            m.insert(pair<int,int>(p[i].idOfTeam,p[i].score));
        else
            itor->second+=p[i].score;
    }
    int maxIdOfTeam=0,maxScore=0;
    for(itor=m.begin() ;itor!=m.end() ;itor++)
    {
        if(itor->second>maxScore)
        {
            maxIdOfTeam=itor->first;
            maxScore=itor->second;
        }
    }
    cout<<maxIdOfTeam<<" "<<maxScore;
}

PAT 乙級 1047 編程團體賽