1141 PAT Ranking of Institutions(附测试点5分析)

题目:

After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:

ID Score School

where ID is a string of 6 characters with the first one representing the test level: B stands for the basic level, A the advanced level and T the top level; Score is an integer in [0, 100]; and School is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that ID is unique for each testee.

Output Specification:

For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:

Rank School TWS Ns

where Rank is the rank (start from 1) of the institution; School is the institution code (all in lower case); ; TWS is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5, where ScoreX is the total score of the testees belong to this institution on level X; and Ns is the total number of testees who belong to this institution.

The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

Sample Input:

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

Sample Output:

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

 

题目大意:给出每个学生的id、分数、学校,学校名称不区分大小写,输出学校排名、学校名称、总加权成绩、学校参赛人数。学校名称输出时候以小写方式输出。

 

思路:

1、因为要根据每个学校的多个属性对学校进行排序,因此将学校的信息使用结构体来存储

2、结构体数组的下标索引要和学校名字一一对应,这里使用map进行映射

3、测试点5要注意存TWS要用double存,比较,输出的时候用int

 

易错点

使用sort对结构体数组进行排序时,要注意数组的大小和下标是否从0开始

 

 

代码:

复制代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
struct School{
    string name;
    double TWS;
    int Ns, tws;
    School(){
        TWS = 0.0;
        Ns = 0;
    }
}school[100005];
map<string, int> mp;
bool cmp(School school1, School school2){
    if(school1.tws != school2.tws){
        return school1.tws > school2.tws;
    }
    else if(school1.Ns != school2.Ns){
        return school1.Ns < school2.Ns;
    }
    else{
        return school1.name < school2.name;
    }
}
int main(){
    int N, index = 0;
    scanf("%d", &N);
    for(int i = 0; i < N; i++){
        string id, name;
        int score;
        cin>>id>>score>>name;
        for(int j = 0; j < name.size(); j++){
            name[j] = tolower(name[j]);
        }
        if(!mp[name]){
            mp[name] = ++index;
        }
        int t = mp[name];
        school[t].name = name;
        school[t].Ns++;
        if(id[0] == 'T'){
            school[t].TWS = school[t].TWS + score * 1.0 * 1.5;
        }else if(id[0] == 'A'){
            school[t].TWS = school[t].TWS + score * 1.0;
        }else{
            school[t].TWS = school[t].TWS + score * 1.0 / 1.5;
        }
    }
    for(int i = 1; i <= index; i++){
        school[i].tws = (int)school[i].TWS;
    }
    sort(school + 1, school + index + 1, cmp);
    printf("%d\n", index);
    int rank = 1;
    for(int i = 1; i <= index; i++){
        if(i > 1 && school[i].tws < school[i - 1].tws){
            rank = i;
        }
        cout<<rank<<" "<<school[i].name<<" "<<school[i].tws<<" "<<school[i].Ns<<endl;
    }
}
复制代码

 

posted @   Yohoc  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示