poj 2643 Election

来源:互联网 发布:latex 矩阵虚线 编辑:程序博客网 时间:2024/05/21 18:33
//这题要注意的两点是:1.注意要用cin.get()输入其结束标志,要不对getline()的输入造成影响//2.要注意选举人的票数相同的情况,例如:最高票数的有两个选举人,那么结果就是tie了! #include <iostream>#include <string>#include <algorithm> using namespace std;struct Info{       string name, party;       int votes; }; bool mycmp(Info a, Info b){     return a.votes > b.votes; } int main(){    int i, j, n, m;    bool flag = false;     string str;     Info info[25];     cin >> n;    cin.get();    for (i = 0; i < n; i++){        getline(cin, info[i].name);        getline(cin, info[i].party);        info[i].votes = 0;     }         cin >> m;    cin.get();     for (i = 0; i < m; i++){        getline(cin, str);         for (j = 0; j < n; j++){            if (info[j].name == str){                 info[j].votes++;                break;             }         }     }         sort(info, info+n, mycmp);     if (info[0].votes == info[1].votes)        flag = true;    if (flag)        cout << "tie" << endl;    else        cout << info[0].party << endl;         system("pause"); } /*3Marilyn MansonRhinocerosJane DoeFamily CoalitionJohn Smithindependent7John SmithMarilyn MansonJane DoeJohn SmithMarilyn MansonJohn SmithJane Doe3Marilyn MansonRhinocerosJane DoeFamily CoalitionJohn Smithindependent3Marilyn MansonJane DoeJohn Smith3Marilyn MansonRhinocerosJane DoeFamily CoalitionJohn Smithindependent6John SmithMarilyn MansonMarilyn MansonJane DoeJohn SmithMM*/