例题5-10 UVA 207 PGA Tour Prize Money PGA巡回赛的奖金

这个题真是快把自己折磨疯了,在学校因为要复习,折腾了10天才AC,结果电脑出了点问题代码数据全部丢失,放假后又写了一遍,结果不断RE一天,唉!

这个题的题意就不说了,紫书上说的很详细了,关键是一点,什么时候分配奖金结束,是等那70个奖金比例全部分配结束后就不分了,哪怕最后一个比例分给了10个并列的,也只分一个,后面就不分了(只要让sum_mon加0就行了!)

整体思路:

1.开一个结构体记录运动员的:4个成绩(dq[5]),姓名(name),奖金(money),名次(place),是否是专业运动员(is_pro),是否输出T(is_T),和前两句的分数总和(all_2),前四局的分数总和(all_4)。

因为输入分数会有字母DQ,不妨设DQ为一个常量99999,这样排序时自动到后面去了

2.输入完成后就是各种排序

3.处理名次和奖金,先找重复的,在给这些重复的定名次和奖金

4.输出结果时,找出各种情况的相同点,来减少代码量!


技巧:

1.将DQ设置为一个很大的常量,让正常分数达不到这个值。

2.读取名字可以用fgets()来读,控制指定的长度(20个字符)。(记得最后在用fgets吃掉换行!)

3.直接用memset给结构体进行初始化为0.(为了简便,将一些改动小的默认为0.例如是否为专业运动员)

4.将比例数值大于70的那些运动员自动设为非职业运动员,这样输出可以直接判断是否为职业运动员来进行输出奖金。

注意:

1.千万注意好访问运动员(结构体)的个数,不要非法访问内存。(RE一天的原因T_T !)

2.在一个奖金平分时会除以个数,注意好别除以0

3.注意奖金要加上精度控制(1e-8就可以了)


代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200;
const int maxt = 50;
const int DQ = 999999;
const double eps = 1e-8;
struct player
{
    char name[maxt];
    double money;
    int place,all_2,all_4,dq[5];
    bool is_pro,is_T;
}players[maxn];
int rev_len(int k){
    int sum = 0;
    while(k!=0){
        k/=10;
        sum++;
    }
    return sum;
}
bool cmp1(const player&a,const player&b){
    if (a.all_2 != b.all_2)return a.all_2 < b.all_2;
    return strcmp(a.name,b.name)<0;
}
bool cmp2(const player&a,const player&b){
    if (a.all_4!=b.all_4)return a.all_4<b.all_4;
    return strcmp(a.name,b.name) < 0;
}
bool cmp3(const player&a,const player&b){
    int suma=0,sumb=0;
    for (int i = 0; i < 4;++i)if (a.dq[i]==DQ){suma=i;break;}
    for (int i = 0; i < 4;++i)if (b.dq[i]==DQ){sumb=i;break;}
    if (suma!=sumb)return suma>sumb;
    if(a.all_4!=b.all_4)return a.all_4<b.all_4;
    return strcmp(a.name,b.name)<0;
}
int main()
{
    //freopen("out.txt","w",stdout);
    int T,n;
    double prot[maxn],money_all;
    scanf("%d",&T);{
        while(T--){
            memset(players,0,sizeof(players));
            memset(prot,0,sizeof(prot));
            scanf("%lf",&money_all);
            for (int i = 0; i < 70; ++i)scanf("%lf",&prot[i]);
            scanf("%d",&n);
            getchar();
            for (int i = 0; i < n; ++i){
                fgets(players[i].name,20,stdin);
                if (!strchr(players[i].name, '*'))players[i].is_pro=true;
                for (int j = 0; j < 4; ++j){
                    int flag = 0;
                    if (!scanf("%d",&players[i].dq[j])){
                        players[i].dq[j] = DQ;
                        flag = 1;
                    }
                if (j<2)players[i].all_2+=players[i].dq[j];
                players[i].all_4+=players[i].dq[j];
                if (flag)break;
                }
                char ch[45];
                fgets(ch,40,stdin);
            }
            sort (players,players+n,cmp1);
            int pos=0,pos2=0;
            while(pos < min(n,70) && players[pos].all_2 < DQ)++pos;
            while(pos < n && players[pos].all_2 == players[pos-1].all_2 && players[pos].all_2 < DQ)++pos;
            sort(players,players+pos,cmp2);
            while(pos2 < n && pos2 <= pos && players[pos2].all_4 < DQ)++pos2;
            while(pos2 < n && players[pos2].all_4 == players[pos2-1].all_4 && players[pos2].all_4<DQ)++pos2;
            if (pos!=pos2)sort(players+min(pos,pos2),players+max(pos,pos2),cmp3);
            int rak=1,cur=0,pos3,cont3=0;
            while(cur < min(pos,pos2) ){
                int sum=0;
                double sum_mon=0;
                for (pos3 = cur; players[pos3].all_4==players[cur].all_4;++pos3){
                    if (players[pos3].is_pro){
                        sum++;
                        sum_mon+=prot[cont3++];
                    }
                }
                if (sum)sum_mon/=sum;
                else sum_mon/=100;
                for (int i = cur; i < pos3; ++i){
                    players[i].place=rak;
                    if (players[i].is_pro && sum)players[i].money=sum_mon*money_all/100.0+eps;
                    if(players[i].is_pro && sum > 1 && cont3-sum<70)players[i].is_T=true;
                    if (cont3-sum >=70)players[i].is_pro=false;
                }
                int numb = pos3-cur;
                rak+=numb;
                cur+=numb;
            }
            printf("Player Name          Place     RD1  RD2  RD3  RD4  TOTAL     Money Won\n");
            printf("-----------------------------------------------------------------------\n");
            for (int i = 0; i < pos; ++i){
                printf("%-21s",players[i].name);
                int N=10;
                if (players[i].all_4<DQ){printf("%d",players[i].place);N-=rev_len(players[i].place);}
                if (players[i].is_T){printf("T");N--;}
                for (int i = 0; i < N; ++i)printf(" ");N=4;
                for (int j =0;j < 4; ++j){
                    if(players[i].dq[j]!=DQ) printf("%-5d",players[i].dq[j]);
                    else {N-=j;break;}
                }if (N==4)N=0;
                for(int i = 0; i < N; ++i)printf("     ");
                if (N){printf("DQ\n");continue;}
                if (players[i].is_pro){printf("%-10d",players[i].all_4);printf("$%9.2lf",players[i].money);}
                else printf("%d",players[i].all_4);
                printf("\n");
            }
            if (T)printf("\n");
        }
    }
    return 0;
}


你可能感兴趣的:(C语言,uva)