PAT (Basic Level) Practice 1085 PAT单位排行

每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。

输入格式:

输入第一行给出一个正整数 N(≤10​5​​),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息:

准考证号 得分 学校

其中准考证号是由 6 个字符组成的字符串,其首字母表示考试的级别:B代表乙级,A代表甲级,T代表顶级;得分是 [0, 100] 区间内的整数;学校是由不超过 6 个英文字母组成的单位码(大小写无关)。注意:题目保证每个考生的准考证号是不同的。

输出格式:

首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜:

排名 学校 加权总分 考生人数

其中排名是该单位的排名(从 1 开始);学校是全部按小写字母输出的单位码;加权总分定义为乙级总分/1.5 + 甲级总分 + 顶级总分*1.5整数部分考生人数是该属于单位的考生的总人数。

学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。

输入样例:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

输出样例:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

算法一:

思路:建立两个结构体,一个Student,记录考生信息,一个School,记录学校信息(学校名称,考生人数,加权总分),每次输入考生信息时,都对比School中是否有该考生所对应的学校信息,如果存在,则考生人数加一,并且计算此时的加权总分。相应的时间复杂度为O(n2),最后两个测试点没有通过,运行超时。

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <ctype.h>
using namespace std;

typedef struct Student{
    char ID[7];
    int grade;
    char school[7];
}Student;

typedef struct School{
    char name[7];
    int num;
    double sumGrade;
}School;

void toLow(char a[]){
    for(int i = 0;i < strlen(a);i++)
        a[i] = tolower(a[i]);
}

int cmp(const void *a , const void *b){
    if((*(School *)a).sumGrade != (*(School *)b).sumGrade)
        return (*(School *)a).sumGrade < (*(School *)b).sumGrade ? 1: -1;
    else if((*(School *)a).num != (*(School *)b).num)
        return (*(School *)a).num > (*(School *)b).num ? 1: -1;
    else
        return strcmp((*(School *)a).name,(*(School *)b).name) > 0 ? 1:-1;
}

int main()
{
    int n,sum = 0;
    char sch[7];
    scanf("%d",&n);
    Student student[n];
    School school[n];
    for(int i = 0;i < n;i++){
        int flag = 0;
        scanf("%s%d%s",student[i].ID,&student[i].grade,sch);
        toLow(sch);
        strcpy(student[i].school,sch);
        for(int j = 0;j < sum;j++){
            if(strcmp(school[j].name,student[i].school) == 0){
                flag = 1;
                school[j].num++;
                if(student[i].ID[0] == 'T')
                    school[j].sumGrade = school[j].sumGrade + student[i].grade*1.5;
                else if(student[i].ID[0] == 'A')
                    school[j].sumGrade = school[j].sumGrade + student[i].grade;
                else if(student[i].ID[0] == 'B')
                    school[j].sumGrade = school[j].sumGrade + student[i].grade/1.5;
                break;
            }
        }
        if(flag == 0){
            strcpy(school[sum].name,student[i].school);
            school[sum].num = 1;
            if(student[i].ID[0] == 'T')
                school[sum].sumGrade = student[i].grade*1.5;
            else if(student[i].ID[0] == 'A')
                school[sum].sumGrade = student[i].grade;
            else if(student[i].ID[0] == 'B')
                school[sum].sumGrade = student[i].grade/1.5;
            sum++;
        }
    }

    qsort(school,sum,sizeof(School),cmp);

    printf("%d\n",sum);
    int temp = 1;
    printf("%d %s %d %d\n",1,school[0].name,(int)school[0].sumGrade,school[0].num);
    for(int i = 1;i < sum;i++){
        if((int)school[i].sumGrade == (int)school[i-1].sumGrade)
            printf("%d %s %d %d\n",temp,school[i].name,(int)school[i].sumGrade,school[i].num);
        else{
            printf("%d %s %d %d\n",i+1,school[i].name,(int)school[i].sumGrade,school[i].num);
            temp = i+1;
        }
    }

    return 0;
}

算法二: 

思路:由于算法一超时,改变统计学校人数的时间,在输入考生信息时同时增加一条school信息,输入结束之后,对school中按照学校名称name进行排序,排序完成之后,再统计学校的考生人数以及加权总分。将统计完成之后的school进行排序输出。此时,时间复杂度为:O(n)。但是,学校中的grade是用double存储的,在进行第二次排序之前如果没有将其转换为int型,对导致最后一个测试点不通过。

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <ctype.h>
using namespace std;

typedef struct Student{
    char ID[7];
    int grade;
    char school[7];
}Student;

typedef struct School{
    char name[7];
    int num;
    double sumGrade;
}School;

void toLow(char a[]){
    for(int i = 0;i < strlen(a);i++)
        a[i] = tolower(a[i]);
}

int cmp1(const void *a , const void *b){
    return strcmp((*(School *)a).name,(*(School *)b).name) > 0 ? 1:-1;
}

int cmp(const void *a , const void *b){
    if((*(School *)a).sumGrade != (*(School *)b).sumGrade)
        return (*(School *)a).sumGrade < (*(School *)b).sumGrade ? 1: -1;
    else if((*(School *)a).num != (*(School *)b).num)
        return (*(School *)a).num > (*(School *)b).num ? 1: -1;
    else
        return strcmp((*(School *)a).name,(*(School *)b).name) > 0 ? 1:-1;
}

int main()
{
    int n;
    char sch[7];
    scanf("%d",&n);
    Student student[n];
    School school[n],temScch[n];

    for(int i = 0;i < n;i++){
        scanf("%s%d%s",student[i].ID,&student[i].grade,sch);
        toLow(sch);
        strcpy(student[i].school,sch);
        strcpy(school[i].name,student[i].school);
            school[i].num = 1;
        if(student[i].ID[0] == 'T')
            school[i].sumGrade = student[i].grade*1.5;
        else if(student[i].ID[0] == 'A')
            school[i].sumGrade = student[i].grade;
        else if(student[i].ID[0] == 'B')
            school[i].sumGrade = student[i].grade/1.5;
    }

    qsort(school,n,sizeof(School),cmp1);
    strcpy(temScch[0].name,school[0].name);
    temScch[0].num = 1;
    temScch[0].sumGrade = school[0].sumGrade;
    int size = 0;
    for(int i = 1;i < n;i++){
        if(strcmp(temScch[size].name,school[i].name) == 0){
            temScch[size].num++;
            temScch[size].sumGrade += school[i].sumGrade;
        }
        else{
            size++;
            strcpy(temScch[size].name,school[i].name);
            temScch[size].num = 1;
            temScch[size].sumGrade = school[i].sumGrade;
        }
    }

    /*for(int i = 0;i < n;i++){
        int flag = 0;
        scanf("%s%d%s",student[i].ID,&student[i].grade,sch);
        toLow(sch);
        strcpy(student[i].school,sch);
        for(int j = 0;j < sum;j++){
            if(strcmp(school[j].name,student[i].school) == 0){
                flag = 1;
                school[j].num++;
                if(student[i].ID[0] == 'T')
                    school[j].sumGrade = school[j].sumGrade + student[i].grade*1.5;
                else if(student[i].ID[0] == 'A')
                    school[j].sumGrade = school[j].sumGrade + student[i].grade;
                else if(student[i].ID[0] == 'B')
                    school[j].sumGrade = school[j].sumGrade + student[i].grade/1.5;
                break;
            }
        }
        if(flag == 0){
            strcpy(school[sum].name,student[i].school);
            school[sum].num = 1;
            if(student[i].ID[0] == 'T')
                school[sum].sumGrade = student[i].grade*1.5;
            else if(student[i].ID[0] == 'A')
                school[sum].sumGrade = student[i].grade;
            else if(student[i].ID[0] == 'B')
                school[sum].sumGrade = student[i].grade/1.5;
            sum++;
        }
    }*/

    qsort(temScch,size+1,sizeof(School),cmp);

    printf("%d\n",size+1);
    int temp = 1;
    printf("%d %s %d %d\n",1,temScch[0].name,(int)temScch[0].sumGrade,temScch[0].num);
    for(int i = 1;i < size+1;i++){
        if((int)temScch[i].sumGrade == (int)temScch[i-1].sumGrade)
            printf("%d %s %d %d\n",temp,temScch[i].name,(int)temScch[i].sumGrade,temScch[i].num);
        else{
            printf("%d %s %d %d\n",i+1,temScch[i].name,(int)temScch[i].sumGrade,temScch[i].num);
            temp = i+1;
        }
    }

    return 0;
}

算法三:

思路:在算法二的基础上进行修改,在第二次排序之前将school中的加权总分取整,再进行排序,此时测试点全部通过。 

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <ctype.h>
using namespace std;

typedef struct Student{
    char ID[7];
    int grade;
    char school[7];
}Student;

typedef struct School{
    char name[7];
    int num;
    double sumGrade;
}School;

void toLow(char a[]){
    for(int i = 0;i < strlen(a);i++)
        a[i] = tolower(a[i]);
}

int cmp1(const void *a , const void *b){
    return strcmp((*(School *)a).name,(*(School *)b).name) > 0 ? 1:-1;
}

int cmp(const void *a , const void *b){
    if((*(School *)a).sumGrade != (*(School *)b).sumGrade)
        return (*(School *)a).sumGrade < (*(School *)b).sumGrade ? 1: -1;
    else if((*(School *)a).num != (*(School *)b).num)
        return (*(School *)a).num > (*(School *)b).num ? 1: -1;
    else
        return strcmp((*(School *)a).name,(*(School *)b).name) > 0 ? 1:-1;
}

int main()
{
    int n;
    char sch[7];
    scanf("%d",&n);
    Student student[n];
    School school[n],temScch[n];

    for(int i = 0;i < n;i++){
        scanf("%s%d%s",student[i].ID,&student[i].grade,sch);
        toLow(sch);
        strcpy(student[i].school,sch);
        strcpy(school[i].name,student[i].school);
            school[i].num = 1;
        if(student[i].ID[0] == 'T')
            school[i].sumGrade = student[i].grade*1.5;
        else if(student[i].ID[0] == 'A')
            school[i].sumGrade = student[i].grade;
        else if(student[i].ID[0] == 'B')
            school[i].sumGrade = student[i].grade/1.5;
    }

    qsort(school,n,sizeof(School),cmp1);
    strcpy(temScch[0].name,school[0].name);
    temScch[0].num = 1;
    temScch[0].sumGrade = school[0].sumGrade;
    int size = 0;
    for(int i = 1;i < n;i++){
        if(strcmp(temScch[size].name,school[i].name) == 0){
            temScch[size].num++;
            temScch[size].sumGrade += school[i].sumGrade;
        }
        else{
            size++;
            strcpy(temScch[size].name,school[i].name);
            temScch[size].num = 1;
            temScch[size].sumGrade = school[i].sumGrade;
        }
    }

    /*for(int i = 0;i < n;i++){
        int flag = 0;
        scanf("%s%d%s",student[i].ID,&student[i].grade,sch);
        toLow(sch);
        strcpy(student[i].school,sch);
        for(int j = 0;j < sum;j++){
            if(strcmp(school[j].name,student[i].school) == 0){
                flag = 1;
                school[j].num++;
                if(student[i].ID[0] == 'T')
                    school[j].sumGrade = school[j].sumGrade + student[i].grade*1.5;
                else if(student[i].ID[0] == 'A')
                    school[j].sumGrade = school[j].sumGrade + student[i].grade;
                else if(student[i].ID[0] == 'B')
                    school[j].sumGrade = school[j].sumGrade + student[i].grade/1.5;
                break;
            }
        }
        if(flag == 0){
            strcpy(school[sum].name,student[i].school);
            school[sum].num = 1;
            if(student[i].ID[0] == 'T')
                school[sum].sumGrade = student[i].grade*1.5;
            else if(student[i].ID[0] == 'A')
                school[sum].sumGrade = student[i].grade;
            else if(student[i].ID[0] == 'B')
                school[sum].sumGrade = student[i].grade/1.5;
            sum++;
        }
    }*/


    for(int i = 0;i < size+1;i++)//将grade取整后再进行排序
        temScch[i].sumGrade = (int)temScch[i].sumGrade;
    qsort(temScch,size+1,sizeof(School),cmp);

    printf("%d\n",size+1);
    int temp = 1;
    printf("%d %s %d %d\n",1,temScch[0].name,(int)temScch[0].sumGrade,temScch[0].num);
    for(int i = 1;i < size+1;i++){
        if((int)temScch[i].sumGrade == (int)temScch[i-1].sumGrade)
            printf("%d %s %d %d\n",temp,temScch[i].name,(int)temScch[i].sumGrade,temScch[i].num);
        else{
            printf("%d %s %d %d\n",i+1,temScch[i].name,(int)temScch[i].sumGrade,temScch[i].num);
            temp = i+1;
        }
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值