10194 Football (aka Soccer)

最近都没有做什么,很懒,也做不出来,干脆看别人的吧

这份代码很漂亮

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <ctype.h>  
  
struct Teams  
{  
    char name[35];  
    int a_rank,  
        b_point,  
        c_games,  
        d_wins,  
        e_ties,  
        f_losses,  
        g_dif,  
        h_sco,  
        i_aga;  
}teams[35];  
int n,t,g;  
char tournament[110],line[110];  
  
int getNamePos(char *name)  
{  
    for(int i=0;i<t;i++)  
    {  
        if(strcmp(name,teams[i].name)==0)  
            return i;  
    }  
    return -1;  
}  
  
void deal(char *name1,char *name2,int goal1,int goal2)  
{  
    int pos1,pos2;  
    pos1=getNamePos(name1);  
    pos2=getNamePos(name2);  
    teams[pos1].c_games++;  
    teams[pos2].c_games++;  
    teams[pos1].h_sco+=goal1;  
    teams[pos2].h_sco+=goal2;  
    teams[pos1].i_aga+=goal2;  
    teams[pos2].i_aga+=goal1;  
    if(goal1>goal2)  
    {  
        teams[pos1].d_wins++;  
        teams[pos2].f_losses++;  
    }  
    else if(goal1<goal2)  
    {  
        teams[pos2].d_wins++;  
        teams[pos1].f_losses++;  
    }  
    else  
    {  
        teams[pos1].e_ties++;  
        teams[pos2].e_ties++;  
    }  
}  
  
int cmp(const void *a,const void *b)  
{  
    Teams team1=*(Teams *)a,team2=*(Teams *)b;  
    if(team1.b_point!=team2.b_point)  
        return team1.b_point<team2.b_point;  
    else if(team1.d_wins!=team2.d_wins)  
        return team1.d_wins<team2.d_wins;//细节决定成败,WA时,要首先检查一下自己的程序  
    else if(team1.g_dif!=team2.g_dif)  
        return team1.g_dif<team2.g_dif;  
    else if(team1.h_sco!=team2.h_sco)  
        return team1.h_sco<team2.h_sco;  
    else if(team1.c_games!=team2.c_games)  
        return team1.c_games>team2.c_games;  
    else   
    {  
        char f1[35];  
        char f2[35];  
        int i;  
        for(i=0;i<=strlen(team1.name);i++)  
            f1[i]=tolower(team1.name[i]);  
        for(i=0;i<=strlen(team2.name);i++)  
            f2[i]=tolower(team2.name[i]);  
        return strcmp(f1,f2);  
    }  
    return false;  
}  
  
int main()  
{  
    /* 
    freopen("10194.in","r",stdin); 
    freopen("10194.out","w",stdout); 
    //*/  
    scanf("%d\n",&n);  
    while(n--)  
    {  
        memset(teams,0,sizeof(teams));  
        gets(tournament);  
        scanf("%d\n",&t);  
        for(int i=0;i<t;i++)  
            gets(teams[i].name);  
        scanf("%d\n",&g);  
        //printf("%d%d%d",n,t,g);  
        for(int i=0;i<g;i++)  
        {  
            gets(line);  
            int len=strlen(line);  
            char name1[35],name2[35];  
            int goal1=0,goal2=0;  
            int len1=0,len2=0;  
            int flag=0;  
            for(int j=0;j<len;j++)  
            {  
                if(line[j]=='#' || line[j]=='@')  
                    flag++;  
                else   
                {  
                    switch(flag)  
                    {  
                    case 0:  
                        name1[len1++]=line[j];  
                        break;  
                    case 1:  
                        goal1=goal1*10+line[j]-'0';  
                        break;  
                    case 2:  
                        goal2=goal2*10+line[j]-'0';  
                        break;  
                    case 3:  
                        name2[len2++]=line[j];  
                        break;  
                    default:  
                        break;  
                    }  
                }  
            }  
            name1[len1]='\0';  
            name2[len2]='\0';  
            deal(name1,name2,goal1,goal2);  
        }  
        for(int i=0;i<t;i++)  
        {  
            teams[i].b_point=3*teams[i].d_wins+1*teams[i].e_ties;  
            teams[i].g_dif=teams[i].h_sco-teams[i].i_aga;  
        }  
        qsort(teams,t,sizeof(teams[0]),cmp);//往后看,与C++中的sort()的效果正好相反  
        printf("%s\n",tournament);  
        for(int i=0;i<t;i++)  
            printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",  
            i+1,teams[i].name,teams[i].b_point,teams[i].c_games,  
            teams[i].d_wins,teams[i].e_ties,teams[i].f_losses,  
            teams[i].g_dif,teams[i].h_sco,teams[i].i_aga);  
        if(n!=0)  
            printf("\n");  
    }  
    return 0;  
} 
#include<iostream>
#include<sstream>
#include<vector>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cctype>
using namespace std;

struct Type
{
    Type()
    {
        name_="";
        score_=win_=lose_=draw_=goal_=all_goal_=game_=0;
    }
    string name_;
    int score_,win_,lose_,draw_,goal_,all_goal_,game_;
};

int n;
vector<Type> r;

int Team(const string &team_name)
{
    for(int i=0;i<n;i++)
        if(r[i].name_==team_name)
            return i;
}

bool cmp_1(const Type &a,const Type &b)
{
    string aa(a.name_),bb(b.name_);
    for(int i=0;i<aa.size();i++)
        aa[i]=tolower(aa[i]);
    for(int i=0;i<bb.size();i++)
        bb[i]=tolower(bb[i]);
    return aa<bb;
}
bool cmp_2(const Type &a,const Type &b)
{
    return a.game_<b.game_;
}
bool cmp_3(const Type &a,const Type &b)
{
    return a.all_goal_>b.all_goal_;
}
bool cmp_4(const Type &a,const Type &b)
{
    return a.goal_>b.goal_;
}
bool cmp_5(const Type &a,const Type &b)
{
    return a.win_>b.win_;
}
bool cmp_6(const Type &a,const Type &b)
{
    return a.score_>b.score_;
}

int main()
{
    int T;
    cin>>T;
    getchar();
    bool first(true);
    while(T--)
    {
        string game_name;
        getline(cin,game_name);
        cin>>n;
        getchar();
        r.resize(n);
        for(int i=0;i<n;i++)
            getline(cin,r[i].name_);
        int g;
        cin>>g;
        getchar();
        string s;
        for(int i=0;i<g;i++)
        {
            getline(cin,s);
            int pos_1(s.find('#')),pos_2(s.rfind('#'));
            string team_1(s.substr(0,pos_1)),team_2(s.substr(pos_2+1));
            istringstream sin(s.substr(pos_1+1));
            int goal_1,goal_2;
            sin>>goal_1;
            char ch;
            sin>>ch;
            sin>>goal_2;
            int team_1_num(Team(team_1)),team_2_num(Team(team_2));
            r[team_1_num].all_goal_+=goal_1;
            r[team_1_num].goal_+=(goal_1-goal_2);
            r[team_1_num].game_++;
            r[team_2_num].all_goal_+=goal_2;
            r[team_2_num].goal_+=(goal_2-goal_1);
            r[team_2_num].game_++;
            if(goal_1>goal_2)
            {
                r[team_1_num].score_+=3;
                r[team_1_num].win_++;
                r[team_2_num].lose_++;
            }
            else if(goal_1<goal_2)
            {
                r[team_2_num].score_+=3;
                r[team_2_num].win_++;
                r[team_1_num].lose_++;
            }
            else
            {
                r[team_1_num].score_++;
                r[team_2_num].score_++;
                r[team_1_num].draw_++;
                r[team_2_num].draw_++;
            }
        }

        sort(r.begin(),r.end(),cmp_1);
        stable_sort(r.begin(),r.end(),cmp_2);
        stable_sort(r.begin(),r.end(),cmp_3);
        stable_sort(r.begin(),r.end(),cmp_4);
        stable_sort(r.begin(),r.end(),cmp_5);
        stable_sort(r.begin(),r.end(),cmp_6);

        if(first)
            first=false;
        else
            cout<<endl;
        cout<<game_name<<endl;
        for(int i=0;i<n;i++)
            printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",i+1,r[i].name_.c_str(),r[i].score_,r[i].game_,r[i].win_,r[i].draw_,r[i].lose_,r[i].goal_,r[i].all_goal_,r[i].all_goal_-r[i].goal_);

        fill_n(r.begin(),n,Type());
    }

    return 0;
}
原文地址:https://www.cnblogs.com/UnGeek/p/2595906.html