结构体综合运用

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2057&cid=1156


#include <iostream>  
#include <algorithm>  
#include <vector>  
#include <string>  
#include <functional>  
  
using namespace std;  
  
typedef struct contest  
{  
    string name;  
    int score;  
} ctst;  
  
bool operator >(const ctst &a, const ctst &b)  
{  
    return a.score > b.score;  
}  
  
int main()  
{  
    int n;  
    cin>>n;  
    vector<ctst> a;  
  
    while(n--)  
    {  
        ctst temp;  
        cin>>temp.name>>temp.score;  
        a.push_back(temp);  
    }  
    char command;  
    while(cin>>command)  
    {  
        ctst temp;  
        switch (command)  
        {  
  
        case 'A':  
            cin>>temp.name>>temp.score;  
            a.push_back(temp);  
            break;  
  
        case 'Q':  
            cin>>temp.name;  
            for(vector<ctst>::iterator ite = a.begin(); ite != a.end(); ++ite)  
            {  
                if((*ite).name == temp.name)  
                {  
                    a.erase(ite);  
                }  
            }  
            break;  
  
        case 'C':  
            cin>>temp.name>>temp.score;  
            for(vector<ctst>::iterator ite = a.begin(); ite != a.end(); ++ite)  
            {  
                if((*ite).name == temp.name)  
                {  
                    (*ite).score += temp.score;  
                }  
            }  
            break;  
  
        case 'S':  
            sort(a.begin(),a.end(),greater<ctst>());  
            for(vector<ctst>::iterator ite = a.begin(); ite != a.end(); ++ite)  
            {  
                cout<<(*ite).name<<' '<<(*ite).score<<endl;  
            }  
            cout<<endl;  
            break;  
  
        case 'O':  
            sort(a.begin(),a.end(),greater<ctst>());  
            int mark = 1;  
            int i = 1;  
            vector<ctst>::iterator marker = a.begin();  
            for(vector<ctst>::iterator ite = a.begin() ; mark < 7; ++ite)  
            {  
                if((*(ite)).score != (*(ite+1)).score)  
                {  
                    if(mark == 1 || mark == 2 || mark == 4)  
                    {  
                        cout<<"#"<<i++<<" :";  
                    }  
                    mark++;  
                    for(vector<ctst>::iterator it = marker; it <= ite; ++it)  
                    {  
                        cout<<' '<<(*it).name;  
                    }  
                    if(mark == 1 || mark == 2 || mark == 4)  
                    {  
                        cout<<endl;  
                    }  
                    marker = ite + 1;  
                }  
            }  
            cout<<endl;  
            break;  
        }  
        if(command == 'O')  
        {  
           break;  
        }  
    }  
}  


你可能感兴趣的:(ACM题解报告)