PAT1012 The Best Rank

该系统针对计算机科学一年级学生的C语言编程、数学和英语三门课程成绩进行综合排名,并为每位学生展示其最佳排名,包括平均成绩、C语言编程、数学和英语单项排名。

1012 The Best Rank (25分)
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID C M E A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output Specification:
For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:
1 C
1 M
1 E
1 A
3 A
N/A

虽然100+行,但是条理清晰,希望有人看得懂。
写代码十分钟,debug两小时<->

#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

const int MAXN=2010;
struct Student{
    string id;
    int a,c,m,e;
}stu[MAXN];

int n,m;

bool cmp1(Student stu1,Student stu2){
    return stu1.a>stu2.a;
}

int rk1(int k){
    Student st[MAXN];
    for(int i=0;i<n;i++)
        st[i]=stu[i];
    sort(st,st+n,cmp1);
    int ranking=0;
    while(st[ranking].a!=stu[k].a){
        ranking++;
        //printf("r=%d\n",ranking);
    }
    ranking++;
    return ranking;
}

bool cmp2(Student stu1,Student stu2){
    return stu1.c>stu2.c;
}

int rk2(int k){
    Student st[MAXN];
    for(int i=0;i<n;i++)
        st[i]=stu[i];
    sort(st,st+n,cmp2);
    int ranking=0;
    while(st[ranking].c!=stu[k].c){
        ranking++;
    }
    ranking++;
    return ranking;
}

bool cmp3(Student stu1,Student stu2){
    return stu1.m>stu2.m;
}

int rk3(int k){
    Student st[MAXN];
    for(int i=0;i<n;i++)
        st[i]=stu[i];
    sort(st,st+n,cmp3);
    int ranking=0;
    while(st[ranking].m!=stu[k].m){
        ranking++;
    }
    ranking++;
    return ranking;
}

bool cmp4(Student stu1,Student stu2){
    return stu1.e>stu2.e;
}

int rk4(int k){
    Student st[MAXN];
    for(int i=0;i<n;i++)
        st[i]=stu[i];
    sort(st,st+n,cmp4);
    int ranking=0;
    while(st[ranking].e!=stu[k].e){
        ranking++;

    }
    ranking++;
    return ranking;
}

void seekbest(string name){
    int pos=0;
    while(pos<n&&stu[pos].id!=name){
        pos++;
        //cout<<name<<endl;
    }
    if(pos==n){
        printf("N/A");
        return ;
    }
    int ar=rk1(pos);
    int cr=rk2(pos);
    int mr=rk3(pos);
    int er=rk4(pos);
    //printf("pos=%d %d %d %d %d ",pos,ar,cr,mr,er);
    if(min(min(ar,cr),min(mr,er))==ar){
        printf("%d A",ar);
        return ;
    }
    if(min(min(ar,cr),min(mr,er))==cr){
        printf("%d C",cr);
        return;
    }
    if(min(min(ar,cr),min(mr,er))==mr){
        printf("%d M",mr);
        return ;
    }
    if(min(min(ar,cr),min(mr,er))==er){
        printf("%d E",er);
        return ;
    }
}

int main(){
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++){
        cin>>stu[i].id;
        scanf("%d %d %d",&stu[i].c,&stu[i].m,&stu[i].e);
        stu[i].a=(stu[i].c+stu[i].m+stu[i].e)/3;
    }
    string query[MAXN];
    for(int i=0;i<m;i++){
        cin>>query[i];
    }
    for(int i=0;i<m;i++){
        seekbest(query[i]);
        if(i!=m-1) printf("\n");
    }
    return 0;
}

/*
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999
*/

### 关于 PAT 1012 数字分类 测试点8 的解决方案与错误原因分析 对于 PAT 1012 数字分类中的测试点8,其主要难点在于输入数据的特殊性和边界条件处理不当可能导致的结果不准确或程序崩溃。以下是对此问题的具体分析: #### 输入数据特性 测试点8通常涉及极端情况下的输入数据,例如: - 所有输入均为特定类型的数字(如全是完全平方数),或者完全没有符合条件的数字。 - 存在大量重复的数据项,导致某些类别的数量极大。 这些特殊情况可能会引发浮点数精度误差、除零异常等问题[^1]。 #### 常见错误原因 1. **未正确判断类别为空的情况** 如果某类数字不存在任何满足条件的数值,在计算平均值时容易发生除零错误。因此需要特别注意当某一类的数量为0时如何输出"N"作为替代结果。 2. **数据类型选择不合理** 使用整型变量存储需保留两位小数的平均值会导致舍入误差累积,最终影响输出准确性。应采用双精度浮点数(double)来保存中间计算结果并控制最后输出格式。 3. **交错计算逻辑缺陷** 当前算法可能存在对交替出现的不同种类计数器更新时机把握不准的现象,特别是在连续多个相同类型之后突然切换到另一类型时可能出现遗漏统计现象。建议每次读取新数后立即决定归属哪一类,并同步增加相应计数器值。 4. **初始状态设定不足** 若初始化阶段未能给各组总和设置恰当起始值得话(比如设成非零),则后续累加操作会引入额外偏差。务必保证所有累计量都从真正意义上的“零”开始累加。 #### 改进建议及修正措施 为了应对上述提到的各种潜在陷阱,可以采取如下策略优化代码结构: ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; double sumA=0,sumB=0,sumC=0,sumD=0; // Use double type to store sums. int cntA=0,cntB=0,cntC=0,cntD=0; for(int i=0;i<n;i++){ int num; cin>>num; if(num%5==0 && num%2==0){ sumA += num; cntA++; } else if(num%5==1){ sumB += ((i%2)*(-2)+1)*(double)(num); cntB++; } else if(num%5==2){ sumC += num; cntC++; } else if(num%5==3){ sumD += num; cntD++; } } auto output=[&](char flag,double total,int count)->void{ if(count>0){ printf("%.1lf ",total/count); } else { cout <<flag<<' '; } }; output('N',sumA,cntA); if(cntB!=0){printf("%.1lf ",sumB);}else{cout<<"N ";}; output('N',sumC,cntC); if(cntD!=0){printf("%.1lf\n",sqrt(sumD/cntD));}else{cout<<"N"<<endl;}; } ``` 此版本改进之处包括但不限于以下几个方面: - 更改求解第二类平均的方法,避免简单算术平均带来的可能溢出风险; - 对每一类分别定义独立函数用于统一管理输出行为,增强可维护性的同时减少冗余代码; - 加强了针对空集合情形下返回字符 'N' 的一致性保障机制。 以上即是对PAT 1012 数字分类题目中关于测试点8常见问题及其解决办法的一个较为全面深入探讨。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值