首页 > 其他 > 详细

1004 成绩排名 (20 point(s))

时间:2021-09-05 20:12:43      阅读:31      评论:0      收藏:0      [点我收藏+]
#include <bits/stdc++.h>
using namespace std;

struct Stu{
	string name, id;
};

int main() {
	int n, max = 0, min = 100;
	map<int, Stu> stu;
	cin >> n;
	
	while(n--){
		int score;
		string name, id;
		cin >> name >> id >> score;
		
		stu[score] = {name, id};
		if(score > max) max = score;
		if(score < min) min = score;
		
	}
	
	for(auto s: stu)
		if(s.first == max)
			cout << s.second.name << " " << s.second.id << endl;
	for(auto s: stu)
		if(s.first == min)
			cout << s.second.name << " " << s.second.id << endl;
}
#include <bits/stdc++.h>
using namespace std;

struct Stu{
	string name, id;
};

int main() {
	int n, max = 0, min = 100;
	map<int, Stu> stu;
	cin >> n;
	
	while(n--){
		int score;
		string name, id;
		cin >> name >> id >> score;
		
		stu[score] = {name, id};
	}
	
	cout << stu.rbegin()->second.name << " " << stu.rbegin()->second.id << endl;
	cout << stu.begin()->second.name << " " << stu.begin()->second.id << endl;
}

cout << end(stu)->second.name;

end() 指向的尾部实际是下一个,即将输入元素(实际不存在)的位置。用的话就会不知道输出什么东西。

虽然之前用向量 vector 还是什么的时候错过一次,但是这一次还是没能够想起来,所以换了其他方法ac。而关键是我记得 end() 是尾部的地址,但是用错了rbegin(map) 。因为 rbegin() 不是静态而是成员方法,所以只能 . 点运算符来引用这个方法, map.rbegin() 。

rbegin()

1004 成绩排名 (20 point(s))

原文:https://www.cnblogs.com/Atl212/p/15226248.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!