1. 程式人生 > 其它 >1004 成績排名 (20 point(s))

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

#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()