文章

35

粉丝

0

获赞

144

访问

7.2k

头像
这题不严格按照10位,投机取巧不行,直接find找分割
P1542 云南大学/暨南大学机试题
发布于2025年3月16日 16:42
阅读数 199

#include <bits/stdc++.h>
using namespace std;

bool isleap(int year) {
    // 判断是否是闰年
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        return true;
    }
    return false;
}

void getday(string s) {
    // 查找'-'来提取年月日
    int firstDash = s.find('-');
    int secondDash = s.find('-', firstDash + 1);

    // 提取年份、月份和日期
    int year = stoi(s.substr(0, firstDash));            // 从0到firstDash提取年份
    int month = stoi(s.substr(firstDash + 1, secondDash - firstDash - 1)); // 年月之间
    int day = stoi(s.substr(secondDash + 1));           // 日期从第二个'-'后开始

    // 存储每个月的天数
    int monthDay[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    // 如果是闰年,修改二月的天数
    if (isleap(year)) {
        monthDay[1] = 29;  // 二月天数改为29
    }

    int totalDays = 0;
    // 累加该日期之前的所有天数
    for (int i = 0; i < month - 1; i++) {
        totalDays += monthDay[i];
    }

    // 输出该日期是这一年的第几天
    cout << totalDays + day <...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发