AcWing_1229 日期问题(字符串处理、日期、set / 枚举)

AcWing

1229. 日期问题

读数据:scanf("%d/%d/%d", &a, &b, &c);

输出结果:printf("%d-%02d-%02d\n", a, b, c);,表示长度为2,长度不足2的部分用0填补(只有0能这样用,默认是空格填补).

set实现排序。

也可以按所有的日期,从1960010120591231顺序枚举,如果日期合法并且可以匹配题目给出的格式,那么就输出,这样可以不用专门再排序。

#include <bits/stdc++.h>
using namespace std;
set<int> res;
bool judgeleap(int year){
    if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return true;
    return false;
}
bool check(int year, int month, int day){
    int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(month == 0 || month > 12 || day == 0) return false;
    if(month != 2 && day > days[month]) return false;
    if(month == 2 && judgeleap(year) && day > 29) return false;
    if(month == 2 && !judgeleap(year) && day > 28) return false;
    return true;
}
void calc(int a, int b, int c){
    int year, month = b, day = c;
    if(a < 60) year = 2000 + a;
    else year = 1900 + a;
    if(!check(year, month, day)) return;
    res.insert(year * 10000 + month * 100 + day);
}
int main(){
    int a, b, c;
    scanf("%d/%d/%d", &a, &b, &c);
    calc(a, b, c);
    calc(c, a, b);
    calc(c, b, a);
    for(auto t : res) printf("%d-%02d-%02d\n", t / 10000, t / 100 % 100, t % 100);
    return 0;
}