侧边栏壁纸

【C++】结构体2

2024年06月11日 194阅读 0评论 0点赞

lxam0l4u.png

用到的题-->P1104 生日 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

关于sort

stable_sort:遇到相同的元素 不调换位置
sort:反之

示例代码

/*
结构体  --  2
结构体排序
洛谷P1104

---

stable_sort:遇到相同的元素 不调换位置
sort:反之
*/

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
using namespace std;

struct stu
{
    string name;
    int y, m, d;
}a[105];

bool cmp(stu x, stu y) {
    if (x.y != y.y)
    {
        return x.y < y.y;
    }
    else
    {
        if (x.m != y.m)
        {
            return x.m < y.m;
        }
        else if (x.d != y.d)
        {
            return x.d < y.d;
        }
    }
    return true;
}

int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].name >> a[i].y >> a[i].m >> a[i].d;
    }
    stable_sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        cout << a[i].name << endl;
    }
    return 0;
}
0
打赏

—— 评论区 ——

昵称
邮箱
网址
取消
博主栏壁纸
25 文章数
31 标签数
5 评论量
人生倒计时
最新评论
  • 人气很差!一条评论也没有!
舔狗日记