PAT 乙级 1085.PAT单位排行 C++/Java

题目来源

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

输入格式:

输入第一行给出一个正整数 N(≤105),即考生人数。随后 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

分析:

存储数据

typedef struct Student {
    string id;			// 准考证号
    double score;		// 得分
    string school;		// 学校
};

typedef struct School {
    int ranking;		// 排名
    string schoolName;	// 学校
    double schoolScore;	// 加权总分
    int studentCnt;		// 考生人数

};

输出样例中,学校名都是小写的,所以接收输入的时候,也要把学校名转成小写再存起来

Q:怎么记录学校中有多少个考生,总分是多少呢?

A:把它们存进unordered_map<string, School> m里面,key是学校名,value是学校结构体

Q:存进unordered_map之后,怎么排序?

A:遍历unorder_map,由于它的value是一个School结构体,所以把value存进vector<School>的数组里,再进行排序

Q:并列排名要怎么输出成1 1 3 4 4的情况?

A:School记录着学校排名,先给第一个ranking赋值为1,先遍历vector<Schhol>数组,i 从 0 到 学校的数量N,如果当前分数和上一个相同,那么排名就和上一个相同。否则,排名就是 i + 1

还需要注意的是,分数是double类型,输出的时候只输出整数部分

C++实现:

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

typedef struct Student {
    string id;			// 准考证号
    double score;		// 得分
    string school;		// 学校
};

typedef struct School {
    int ranking;		// 排名
    string schoolName;	// 学校
    double schoolScore;	// 加权总分
    int studentCnt;		// 考生人数
};

// 转小写
void toLowerCase(string &str) {
    int len = str.size();
    for (int i = 0; i < len; ++i) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] += 32;
        }
    }
}

// 排序
bool cmp(School& a, School& b) {
    if (a.schoolScore != b.schoolScore) {
        return a.schoolScore > b.schoolScore;
    }
    else if (a.studentCnt != b.studentCnt) {
        return a.studentCnt < b.studentCnt;
    }
    else {
        return a.schoolName < b.schoolName;
    }
}

int main() {
    int N;
    cin >> N;

    vector<Student> stu(N);
    vector<School> schools;
    unordered_map<string, School> m;
    for (int i = 0; i < N; ++i) {
        cin >> stu[i].id >> stu[i].score >> stu[i].school;
        toLowerCase(stu[i].school);

        m[stu[i].school].studentCnt++;
        m[stu[i].school].schoolName = stu[i].school;

        char c = stu[i].id[0];
        if (c == 'T') {
            m[stu[i].school].schoolScore += stu[i].score * 1.5;
        }
        else if (c == 'A') {
            m[stu[i].school].schoolScore += stu[i].score;
        }
        else {
            m[stu[i].school].schoolScore += stu[i].score / 1.5;
        }
    }
    
    // 把value放进数组
    for (auto& i : m) {
        i.second.schoolScore = (int)i.second.schoolScore;
        schools.push_back({ 0,i.second.schoolName,i.second.schoolScore,i.second.studentCnt });
    }

    sort(schools.begin(), schools.end(), cmp);
    
    cout << schools.size() << endl;
    for (int i = 0; i < schools.size(); ++i) {
        if (i != 0 && schools[i].schoolScore == schools[i - 1].schoolScore) {
            schools[i].ranking = schools[i - 1].ranking;
        }
        else {
            schools[i].ranking = i + 1;
        }
        printf("%d %s %.0lf %d\n", 
            schools[i].ranking, 
            schools[i].schoolName.c_str(), 
            schools[i].schoolScore, 
            schools[i].studentCnt);
    }
    return 0;
}

C++实现2:

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
//1085:PAT单位排行
typedef struct School {
	string name;
	int total;
	int stuNum;
};
bool cmp(School a,School b) {
	if (a.total != b.total) return a.total > b.total;
	else if (a.stuNum != b.stuNum) return a.stuNum < b.stuNum;
	else return a.name < b.name;
}
int main() {
	int n;
	cin >> n;
	string id, school;
	double sc;
	map<string, double> scoreArr;
	map<string, int> stuArr;
	for (int i = 0; i < n; i++) {
		cin >> id >> sc >> school;
		for (int j = 0; j < school.length(); j++) {
			school[j] = tolower(school[j]);
		}
		if (id[0] == 'B') sc = sc / 1.5;
		else if (id[0] == 'T') sc = sc * 1.5;
		scoreArr[school] += sc;
		stuArr[school]++;
	}
	vector<School> schArr;
	for (auto it = scoreArr.begin(); it != scoreArr.end(); it++) {
		schArr.push_back(School{ it->first,(int)scoreArr[it->first],stuArr[it->first] });
	}
	sort(schArr.begin(), schArr.end(), cmp);
	cout << schArr.size() << endl;
	int No = 1, current = schArr[0].total;
	for (int i = 0; i < schArr.size(); i++) {
		if(schArr[i].total != current){
			No = i + 1;
			current = schArr[i].total;
		}
		cout << No << " " << schArr[i].name << " " << schArr[i].total << " " << schArr[i].stuNum << endl;
	}
	return 0;
}

Java实现:

// TODO
posted @ 2020-05-25 00:57  47的菠萝~  阅读(116)  评论(0编辑  收藏  举报