NO.39十六届蓝桥杯备战|结构体八道练习|加号小于号运算符重载|自定义排序(C++)

发布于:2025-03-16 ⋅ 阅读:(16) ⋅ 点赞:(0)
加号运算符重载_牛客题霸_牛客网
#include <iostream>
using namespace std;
  
class Time {
  
    public:
        int hours;      // 小时
        int minutes;    // 分钟
  
        Time() {
            hours = 0;
            minutes = 0;
        }
  
        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }
  
        void show() {
            cout << hours << " " << minutes << endl;
        }
  
        // write your code here......
        Time operator+(Time& t)
        {
            Time ret;
            int flg = 0;
            ret.minutes = minutes + t.minutes;
            if (ret.minutes >= 60)
            {
                ret.minutes -= 60;
                flg = 1;
            }
            ret.hours = hours + t.hours + flg;
  
            return ret;
        }
  
};
  
int main() {
  
    int h, m;
    cin >> h;
    cin >> m;
  
    Time t1(h, m);
    Time t2(2, 20);
  
    Time t3 = t1 + t2;
    t3.show();
    return 0;
}
重载小于号_牛客题霸_牛客网
#include <iostream>
using namespace std;
  
class Time {
  
    public:
        int hours;      // 小时
        int minutes;    // 分钟
  
        Time() {
            hours = 0;
            minutes = 0;
        }
  
        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }
  
        void show() {
            cout << hours << " " << minutes << endl;
        }
  
        // write your code here......
        bool operator< (Time& t)
        {
            if (hours != t.hours)
                return hours < t.hours;
            return minutes < t.minutes;
        }
  
};
  
int main() {
    int h, m;
    cin >> h;
    cin >> m;
  
    Time t1(h, m);
    Time t2(6, 6);
    if (t1<t2) cout<<"yes"; else cout<<"no";
    return 0;
}
P5742 【深基7.例11】评等级 - 洛谷
#include <bits/stdc++.h>
using namespace std;

class Stu
{
public:
    int id;
    int score1;
    int score2;

    int calc_total()
    {
        return score1 + score2;
    }
    double calc_zonghe()
    {
        return score1 * 0.7 + score2 * 0.3;
    }
};

void judge (Stu s)
{
    if (s.calc_total() > 140 && s.calc_zonghe() >= 80)
        cout << "Excellent" << endl;
    else
        cout << "Not excellent" << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    Stu s;
    while (n--)
    {
        cin >> s.id >> s.score1 >> s.score2;
        judge(s);
    }
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

class Stu
{
public:
    int id;
    int score1;
    int score2;

    int calc_total()
    {
        return score1 + score2;
    }
    double calc_zonghe()
    {
        return score1 * 0.7 + score2 * 0.3;
    }
};

bool judge (Stu s)
{
    if (s.calc_total() > 140 && s.calc_zonghe() >= 80)
        return true;
    else
        return false;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    Stu s;
    while (n--)
    {
        cin >> s.id >> s.score1 >> s.score2;
        if (judge(s))
            cout << "Excellent" << endl;
        else
            cout << "Not excellent" << endl;
    }

    return 0;
}
B2125 最高分数的学生姓名 - 洛谷
#include <bits/stdc++.h>
using namespace std;

class S
{
public:
    int score;
    string name;
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    S max;
    cin >> max.score >> max.name;
    n--;
    S s;
    while (n--)
    {
        cin >> s.score >> s.name;
        if (s.score > max.score)
            max = s;
    }
    cout << max.name << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
class S
{
public:
    int score;
    string name;
}a[N];

bool cmp(S s1, S s2)
{
    return s1.score > s2.score;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i].score >> a[i].name;
    }
    sort(a, a+n, cmp);
    cout << a[0].name << endl;
    
    return 0;
}
B2131 甲流病人初筛 - 洛谷
#include <bits/stdc++.h>
using namespace std;

class S
{
public:
    string name;
    float tp;
    int flg;
}s;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    int cnt = 0;
    while (n--)
    {
        cin >> s.name >> s.tp >> s.flg;
        if (s.tp >= 37.5 && s.flg)
        {
            cout << s.name << endl;
            cnt++;
        }
    }
    cout << cnt << endl;
    
    return 0;
}
争夺前五名
#include <bits/stdc++.h>
using namespace std;

const int N = 55;
int a[N];

bool cmp(int a, int b)
{
    return a > b;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a, a+n, cmp);
    for (int i = 0; i < 5; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int N = 55;
int a[N];

struct Cmp
{
	bool operator()(int a, int b)
	{
	    return a > b;
	}
}cmp;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a, a+n, cmp);
    for (int i = 0; i < 5; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    
    return 0;
}
P1093 [NOIP 2007 普及组] 奖学金 - 洛谷
#include <bits/stdc++.h>
using namespace std;

const int N = 310;
class S
{
public:
    int chinese;
    int math;
    int english;
    int total;
    int id;
}a[N];

bool cmp(S& s1, S& s2)
{
    if (s1.total != s2.total)
        return s1.total > s2.total;
    else if (s1.chinese != s2.chinese)
        return s1.chinese > s2.chinese;
    else
        return s1.id < s2.id;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].chinese >> a[i].math >> a[i].english;
        a[i].total = a[i].chinese + a[i].math + a[i].english;
        a[i].id = i;
    }
    sort(a+1, a+n+1, cmp);
    for (int i = 1; i <= 5; i++)
    {
        cout << a[i].id << " " << a[i].total << endl;        
    }
    cout << endl;
    
    return 0;
}
P1104 生日 - 洛谷
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
class S
{
public:
    string s;
    int y;
    int m;
    int d;
    int id;
}a[N];

bool cmp(S& s1, S&s2)
{
    if (s1.y != s2.y)
        return s1.y < s2.y;
    else if (s1.m != s2.m)
        return s1.m < s2.m;
    else if (s1.d != s2.d)
        return s1.d < s2.d;
    else
        return s1.id > s2.id;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i].s >> a[i].y >> a[i].m >> a[i].d;
        a[i].id = i;
    }
    sort(a, a+n, cmp);
    for (int i = 0; i < n; i++)
    {
        cout << a[i].s << endl;        
    }
    return 0;
}