曾彪彪的个人网站
首页
文章列表
>>
文章详情
一道简单排序算法题失分总结
作者:
曾彪彪
日期:
2025-08-19 08:09:14
阅读(33)
分类:
问题记录
Algorithm
原题如下: # P1104 生日 ## 题目描述 cjf 君想调查学校 OI 组每个同学的生日,并按照年龄从大到小的顺序排序。但 cjf 君最近作业很多,没有时间,所以请你帮她排序。 ## 输入格式 输入共有 $n + 1$ 行, 第 $1$ 行为 OI 组总人数 $n$; 第 $2$ 行至第 $n+1$ 行分别是每人的姓名 $s$、出生年 $y$、月 $m$、日 $d$。 ## 输出格式 输出共有 $n$ 行, 即 $n$ 个生日从大到小同学的姓名。(如果有两个同学生日相同,输入靠后的同学先输出) ## 输入输出样例 #1 ### 输入 #1 ``` 3 Yangchu 1992 4 23 Qiujingya 1993 10 13 Luowen 1991 8 1 ``` ### 输出 #1 ``` Luowen Yangchu Qiujingya ``` ## 说明/提示 数据保证,$1<n<100$,$1\leq |s|<20$。保证年月日实际存在,且年份 $\in [1960,2020]$。 我的代码如下: ```c++ /** start time: 8:45 End time: Type: simulator Solution: - use struct, define compare, output from backend to previous - - test case: - 3 Yangchu 1992 4 23 Qiujingya 1993 10 13 Luowen 1991 8 1 - 7 Yangchu 1992 4 23 a 1992 4 23 Qiujingya 1993 10 13 Luowen 1991 8 1 x 1991 8 1 y 1991 8 1 z 1991 8 1 - 2 x 1991 8 1 y 1991 8 1 - 1 x 1991 8 1 - first submit score: 100 cause: **/ #include <bits/stdc++.h> using namespace std; struct Student { string name; int year, month, date; }; bool compare(Student s1, Student s2) { if (s1.year == s2.year) { if (s1.month == s2.month) { return s1.date > s2.date; } return s1.month > s2.month; } return s1.year > s2.year; } int main() { // freopen("C:/Users/zengsam/Downloads/P1042_2.in", "r", stdin); int n; cin >> n; vector<Student> students(n); for (int i = 0; i < n; i++) { cin >> students[i].name >> students[i].year >> students[i].month >> students[i].date; } sort(students.begin(), students.end(), compare); for (int i = students.size()-1; i >= 0; i--) { cout << students[i].name << endl; } return 0; } ``` 提交答案,只得84分,通过比较结果发现,在数据量大的情况下,排序结果不稳定。这说明,调用sort方法进行排序,结果可能是不稳定的,需要在struct中再增加一个id,然后根据id进行降序。 调用sort方法进行排序,为什么不稳定呢,因为默认情况下,std::sort使用一种混合排序算法(通常是快速排序、堆排序和插入排序的组合),它不保证稳定性(即相等元素的相对顺序可能会改变)。 调整后的代码如下: ```c++ /** start time: 8:45 End time: Type: simulator Solution: - use struct, define compare, output from backend to previous - - test case: - 3 Yangchu 1992 4 23 Qiujingya 1993 10 13 Luowen 1991 8 1 - 7 Yangchu 1992 4 23 a 1992 4 23 Qiujingya 1993 10 13 Luowen 1991 8 1 x 1991 8 1 y 1991 8 1 z 1991 8 1 - 2 x 1991 8 1 y 1991 8 1 - 1 x 1991 8 1 - first submit score: 100 cause: **/ #include <bits/stdc++.h> using namespace std; struct Student { string name; int id, year, month, date; }; bool compare(Student s1, Student s2) { if (s1.year == s2.year) { if (s1.month == s2.month) { if (s1.date == s2.date) { return s1.id > s2.id; } return s1.date < s2.date; } return s1.month < s2.month; } return s1.year < s2.year; } int main() { // freopen("C:/Users/zengsam/Downloads/P1042_2.in", "r", stdin); int n; cin >> n; vector<Student> students(n); for (int i = 0; i < n; i++) { students[i].id = i; cin >> students[i].name >> students[i].year >> students[i].month >> students[i].date; } sort(students.begin(), students.end(), compare); for (auto s : students) { cout << s.name << endl; } return 0; } ``` 重新提交后,AC。 我记得之前犯过类似的错误,可惜在笔记中找不到了。我们需要认真对待每一道题,不可以想当然。
评论(0)
评论(必填)
名称(必填)
联系方式(可选)
验证码(必填)
提交
评论(必填)
名称(必填)
联系方式(可选)
验证码(必填)