[Blue Bridge Cup Sprint] special training on dates

Table of contents

1. Date accumulation

topic description

enter

output

the code

2. Date difference

topic description

enter

output

the code

3. Print date

topic description

enter

output

the code

Write at the end:


1. Date accumulation

Topic Link: Date Accumulation

topic description

enter

1
2008 2 3 100

output

2008-05-13

the code

#include <iostream>
using namespace std;

//打表
int month[] = {0, 31, 28, 31, 30, 31, 30, 31 , 31, 30, 31, 30, 31};

//取当月日期
int get_mday(int year, int mon) {
    if(mon == 2 && ((year % 4 == 0 && year % 100) || year % 400 == 0)) return month[mon] + 1;
    else return month[mon];
}

//具体实现
void plus_day(int& year, int& mon, int& day, int plus) {
    day += plus;
    while (day > get_mday(year, mon)) {
        int mday = get_mday(year, mon);
        if(day > mday) {
            day -= mday;
            mon++;
        }
        if(mon > 12) {
            mon -= 12;
            year++;
        }
    }
}

int main() 
{
    int n;
    cin >> n;
    while(n--) {
        int year, mon, day, plus;
        cin >> year >> mon >> day >> plus;
        //将日期加在一起
        plus_day(year, mon, day, plus);
        printf("%02d-%02d-%02d\n", year, mon, day);
    }
    return 0;
}

2. Date difference

Topic link: date difference

topic description

enter

20110412
20110422

output

11

the code

#include <iostream>
using namespace std;

//打表
int month[] = {0, 31, 28, 31, 30, 31, 30, 31 , 31, 30, 31, 30, 31};

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

int main() 
{
    int n1, n2;
    while (cin >> n1 >> n2) {
        //保证之后的代码时小的日期减大的日期
        if(n1 > n2) swap(n1, n2);
        else if(n1 == n2) {
            cout << 2 <<endl; //题目要求连续日期算两天
            return 0;
        }

        //取出年月日
        int year1 = n1 / 10000, year2 = n2 / 10000;
        int mon1 = n1 / 100 % 100, mon2 = n2 / 100 % 100;
        int day1 = n1 % 100, day2 = n2 % 100;
        int ans = 0;

        //年份差了几天
        while(year1 != year2) {
            if(is_leap(year1)) ans += 366;
            else ans += 365;
            year1++;
        }

        //月份差了几天
        while(mon1 != mon2) {
            if(mon1 < mon2) {
                if(mon1 == 2 && is_leap(year1)) ans += month[mon1] + 1;
                else ans += month[mon1];
                mon1++;
            }
            else {
                if(mon1 == 2 && is_leap(year1)) ans -= month[mon1] + 1;
                else ans -= month[mon1];
            }
        }

        //日期差了几天
        if(day1 < day2) {
            ans += day2 - day1;
        }
        else {
            ans -= day1 - day2;
        }

        cout << ans + 1 << endl;
    }
    return 0;
}

3. Print date

Topic Link: Print Date

topic description

enter

2000 3
2000 31
2000 40
2000 60
2000 61
2001 60

output

2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01

the code

#include <iostream>
using namespace std;


//打表
int month[] = {0, 31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31};

//求当月日期
int get_mday(int year, int mon) {
    if(mon == 2 && ((year % 4 == 0 && year % 100) || year % 400 == 0)) return month[mon] + 1;
    else return month[mon];
}

int main() 
{
    int year, day;
    while (cin >> year >> day) {
        int mon = 1;
        while (day > get_mday(year, mon)) {
            day -= get_mday(year, mon);
            mon++;
            if(mon > 12) {
                mon -= 12;
                year++;
            }
        }
        printf("%02d-%02d-%02d\n", year, mon, day);
    }
    return 0;
}

Summarize:

The topics of the date class are similar with minor differences.

If you have mastered the basic ideas of the date class, you can basically get the score in this part.

If you are not proficient in date classes, hurry up and practice with the above questions!

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch. 

Guess you like

Origin blog.csdn.net/Locky136/article/details/130009617