第0章 基础问题(1)

这篇博客主要介绍了C++的基础语法题目,包括成绩排序的稳定排序和多关键字排序,以及时间计算类题目如打印日期、日期累加和节日计算。此外,还探讨了棋盘遍历、整数集合划分、三元组的最小距离等思维题和杂题。
摘要由CSDN通过智能技术生成

一、基础语法题目

1.1 成绩排序(稳定排序、多关键字排序)

ACwing 3375

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1010;

int n, m;

struct Student {
   
    string name;
    int score, id;

    bool operator<(const Student &S) const {
   
        if (score != S.score) return score < S.score;
        else return id < S.id;
    }

    bool operator>(const Student &S) const {
   
        if (score != S.score) return score > S.score;
        else return id < S.id;
    }
} student[N];

int main() {
   
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
   
        cin >> student[i].name >> student[i].score;
        student[i].id = i;
    }
    if (!m) sort(student, student + n, greater<Student>()); // 从大到小排序
    else sort(student, student + n);
    for (int i = 0; i < n; i++)
        cout << student[i].name << " " << student[i].score << endl;
    return 0;
}

c++ 自带的稳定排序

stable_sort()

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1010;

int n, m;

struct Stu {
   
    string name;
    int score;

    bool operator<(const Stu &s) const {
   
        return score < s.score;
    }

    bool operator>(const Stu &s) const {
   
        return score > s.score;
    }
} stu[N];

int main() {
   
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> stu[i].name >> stu[i].score;
    if (!m) stable_sort(stu, stu + n, greater<Stu>());
    else stable_sort(stu, stu + n);
    for (int i = 0; i < n; i++)
        cout << stu[i].name << " " << stu[i].score << endl;
    return 0;
}

1.2 时间计算类题目

重要函数

const int month[13] = {
   0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

// 判断是否为闰年,闰年返回1,平年返回0
int is_leap(int year) {
    
    if (year % 4 == 0 && year % 100 || year % 400 == 0) return 1;
    return 0;
}

// y年m月有多少天
int get_days(int y, int m) {
    
    if (m == 2) return month[2] + is_leap(y);
    return month[m];
}

// 从今年这一天到明天这一天所经过的天数
int get_year_day(int y, int m) {
    
    if (m <= 2) return 365 + is_leap(y); // 判断今年是否为闰年
    return 365 + is_leap(y + 1); // 判断明年是否为闰年
}

1.2.1 打印日期

ACwing 3607

#include <iostream>
#include <algorithm>
using namespace std;

const int month[13] = {
   0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int is_leap(int year) {
   
    if (year % 4 == 0 && year % 100 || year % 400 == 0) return 1;
    return 0;
}

int get_days(int y, int m) {
   
    if (m == 2) return month[2] + is_leap(y);
    return month[m];
}

int main() {
   
    int y, s;
    while (cin >> y >> s) {
   
        int m = 1, d = 1;
        s--;
        while (s--) 
            if (++d > get_days(y, m)) {
   
                d = 1; // 重置每月第一天
                if (++m > 12) m = 1, y++; // 重置每年第一天
            }
        printf("%04d-%02d-%02d\n", y, m, d);
    }
    return 0;
}

1.2.2 日期累加

ACwing 3573

#include <iostream>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值