Acwing2024蓝桥杯日期问题

模板:

判断日期是否合理

cpp 复制代码
int flag[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int y,int m,int d){
    if(!m||!d||m>12) return false;
    if(m!=2&&d>flag[m]) return false;
    if(m==2){
        bool leap=(y%400==0||(y%4==0&&y%100!=0));
        if(d>flag[m]+leap) return false;
    }
    return true;
}

题目:

AcWing 3498. 日期差值

cpp 复制代码
#include<iostream>
#include<cmath>
using namespace std;
int flag[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int days(int date){
    int year=date/10000;
    int month=date%10000/100;
    int day=date%100;
    int ans=0;
    for(int i=1;i<year;i++){
        if(i%400==0||(i%4==0&&i%100!=0)) ans+=366;
        else ans+=365;
    }
    if(year%400==0||(year%4==0&&year%100!=0)) flag[2]=29;
    else flag[2]=28;
    for(int i=0;i<month;i++) ans+=flag[i];
    ans+=day;
    return ans;
}
int main(){
    ios::sync_with_stdio(false);
    int date1,date2;
    while(cin>>date1>>date2){
        int num1=days(date1);
        int num2=days(date2);
        cout<<abs(num2-num1)+1<<endl;
    }
    return 0;
}

AcWing 1229. 日期问题(第八届省赛)

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;
string s;
int date[5],cnt;
int flag[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int y,int m,int d){
    if(y>=60&&y<=99) y+=1900;
    else y+=2000;
    if(!m||m>12||!d) return false;
    if(m!=2&&d>flag[m]) return false;
    if(m==2){
        bool leap=(y%400==0||(y%4==0&&y%100!=0));
        if(d>flag[m]+leap) return false;
    }
    return true;
}
void show(int y,int m,int d){
    if(y>=60&&y<=99) y+=1900;
    else y+=2000;
    date[cnt++]=y*10000+m*100+d;
}
int main(){
    cin>>s;
    int a=10*(s[0]-'0')+(s[1]-'0');
    int b=10*(s[3]-'0')+(s[4]-'0');
    int c=10*(s[6]-'0')+(s[7]-'0');
    if(check(a,b,c)) show(a,b,c);
    if(check(c,a,b)) show(c,a,b);
    if(check(c,b,a)) show(c,b,a);
    sort(date,date+cnt);
    for(int i=0;i<cnt;i++){
        if(date[i]!=date[i+1]){
            printf("%d-",date[i]/10000);
            printf("%02d-",date[i]%10000/100);
            printf("%02d\n",date[i]%100);
        }
    }
    return 0;
}

AcWing 466. 回文日期

cpp 复制代码
#include<iostream>
using namespace std;
long long ans;
int flag[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int y,int m,int d){
    if(!m||!d||m>12) return false;
    if(m!=2&&d>flag[m]) return false;
    if(m==2){
        bool leap=(y%400==0||(y%4==0&&y%100!=0));
        if(d>flag[m]+leap) return false;
    }
    return true;
}
bool judge(int y,int m,int d){
    if(y==d%10*1000+d/10*100+m%10*10+m/10) return true;
    else return false;
}
int main(){
    int date1,date2;
    cin>>date1>>date2;
    for(int i=date1;i<=date2;i++){
        int a=i/10000;
        int b=i%10000/100;
        int c=i%100;
        if(check(a,b,c)&&judge(a,b,c)) ans++;
    }
    cout<<ans<<endl;
    return 0;
}

AcWing 2867. 回文日期(第十一届省赛)

cpp 复制代码
#include<iostream>
using namespace std;
int flag[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int y,int m,int d){
    if(!m||!d||m>12) return false;
    if(m!=2&&d>flag[m]) return false;
    if(m==2){
        bool leap=(y%400==0||(y%4==0&&y%100!=0));
        if(d>flag[m]+leap) return false;
    }
    return true;
}
bool judge1(int y,int m,int d){
    if(y==d%10*1000+d/10*100+m%10*10+m/10) return true;
    else return false;
}
bool judge2(int y,int m,int d){
    int a=d%10,b=d/10;
    if(a!=b&&m/10==b&&m%10==a&&y%10==b&&y%100/10==a&&y/1000==a&&y/100%10==b) return true;
    else return false;
}
int main(){
    int date;cin>>date;
    for(int i=date+1;;i++){
        int a=i/10000;
        int b=i%10000/100;
        int c=i%100;
        if(check(a,b,c)&&judge1(a,b,c)){
            cout<<i<<endl;
            break;
        }
    }
    for(int i=date+1;;i++){
        int a=i/10000;
        int b=i%10000/100;
        int c=i%100;
        if(check(a,b,c)&&judge2(a,b,c)){
            cout<<i<<endl;
            break;
        }
    }
    return 0;
}

AcWing 3218. 日期计算

cpp 复制代码
#include<iostream>
using namespace std;
int year,day,months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
    cin>>year;cin>>day;
    if((year%400==0)||(year%4==0&&year%100!=0)) months[2]=29;
    else months[2]=28;
    int i=1;
    while(day>months[i]){
        day-=months[i];
        i++;
    }
    cout<<i<<endl;
    cout<<day<<endl;
    return 0;
}
相关推荐
zylyehuo1 小时前
C++基础编程
c++
猿究院--王升1 小时前
jvm三色标记
java·jvm·算法
一车小面包2 小时前
逻辑回归 从0到1
算法·机器学习·逻辑回归
tt5555555555552 小时前
C/C++嵌入式笔试核心考点精解
c语言·开发语言·c++
lg_cool_2 小时前
Qt 中最经典、最常用的多线程通信场景
c++·qt6.3
科大饭桶3 小时前
C++入门自学Day14-- Stack和Queue的自实现(适配器)
c语言·开发语言·数据结构·c++·容器
tt5555555555553 小时前
字符串与算法题详解:最长回文子串、IP 地址转换、字符串排序、蛇形矩阵与字符串加密
c++·算法·矩阵
元亓亓亓4 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
rainFFrain4 小时前
Boost搜索引擎项目(详细思路版)
网络·c++·http·搜索引擎
不会学习?5 小时前
算法03 归并分治
算法