Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Sunday, October 21, 2012

uva 10420 - List of Conquests


Problem link
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 2010;
//---------------------------------
class person {public: string pos; int value;};
//---------------------------------
int n;
person list[maxn];


//---------------------------------
int main()
{
    //freopen(fi,"r",stdin);
    //freopen(fo,"w",stdout);
    scanf("%d\n",&n);
    int cnt = 0;
    for (int i=0; i<maxn; i++) list[i].pos = "";
    for (int i=1; i<=n; i++)
    {
        string line,country;
        getline(cin,line);
        while (line[0]==' ') line.erase(0,1);
        country = "";
        bool flag = false;
        for (int j=0; j<line.length(); j++)
            if (line[j]!=' ') country = country + line[j];
            else break;
        for (int j=1; j<=cnt; j++)
            if (list[j].pos==country)
            {
               list[j].value++;
               flag = true;
               break;
            }
        if (!flag)
        {
           list[++cnt].pos = country;
           list[cnt].value = 1;
        }
    }
    for (int i=1; i<=cnt-1; i++)
        for (int j=i+1; j<=cnt; j++)
            if (list[i].pos>list[j].pos)
            {
               person tmp;
               tmp = list[i];
               list[i] = list[j];
               list[j] = tmp;
            }
    for (int i=1; i<=cnt; i++)
        cout << list[i].pos << " " << list[i].value << endl;
    return 0;
}
//---------------------------------

No comments:

Post a Comment