描述一个大班级中有 n 名学生存在两个生日相同的概率很高现在给出每个学生的名字出生月份和日期请你编写程序输出所有生日相同的学生。输入描述第一行仅仅包含一个正整数 n 接下来的 n 行每行描述一名学生的信息依次为姓名一个仅含大小写字母长度不超 20 的字符串、出生的月份和出生日子出生的月份和日子符合日历的基本规则各部分两两之间用一个空格分隔。输出描述若干行每行描述一组生日相同的学生信息各行首先是月再
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
struct Student {
string name;
int month;
int day;
};
bool compare(const Student& s1, const Student& s2) {
if (s1.month != s2.month) {
return s1.month < s2.month;
} else if (s1.day != s2.day) {
return s1.day < s2.day;
} else if (s1.name.length() != s2.name.length()) {
return s1.name.length() < s2.name.length();
} else {
return s1.name < s2.name;
}
}
int main() {
int n;
cin >> n;
vector<Student> students;
map<pair<int, int>, vector<string>> birthdayMap;
for (int i = 0; i < n; i++) {
string name;
int month, day;
cin >> name >> month >> day;
students.push_back({name, month, day});
birthdayMap[{month, day}].push_back(name);
}
bool found = false;
for (auto it = birthdayMap.begin(); it != birthdayMap.end(); it++) {
if (it->second.size() > 1) {
found = true;
sort(it->second.begin(), it->second.end(), [](const string& s1, const string& s2) {
if (s1.length() != s2.length()) {
return s1.length() < s2.length();
} else {
return s1 < s2;
}
});
cout << it->first.first << " " << it->first.second;
for (const string& name : it->second) {
cout << " " << name;
}
cout << endl;
}
}
if (!found) {
cout << "None" << endl;
}
return 0;
}
``
原文地址: http://www.cveoy.top/t/topic/iPdS 著作权归作者所有。请勿转载和采集!