1 条题解

  • 0
    @ 2022-07-16 20:23:13

    #include <bits/stdc++.h>
    using namespace std;
    int isLeapYear(int year)
    {
    return year%4==0&&year%100!=0||year%400==0;
    }
    int daysOfMonth(int year,int month)
    {
    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
    {
    return 31;
    }
    else if(month==4||month==6||month==9||month==11)
    {
    return 30;
    }
    else
    {
    if(isLeapYear(year))
    {
    return 29;
    }
    else
    {
    return 28;
    }
    }
    }
    int passDays(int year,int month,int day)
    {
    int m,days=0,s;
    for(m=1;m<month;m++)
    {
    days+=daysOfMonth(year,m);
    }
    return days+day;
    }
    int daysOfYear(int year)
    {
    if(year%4==0&&year%100!=0||year%400==0)
    {
    return 366;
    }
    else
    {
    return 365;
    }
    }
    int weekOfTheFirstDay(int year,int month)
    {
    int y,days=0;
    for(y=1800;y<year;y++)
    {
    days+=daysOfYear(y);
    }
    days+=passDays(year,month,1)-1;
    return (days%7+3)%7;
    }
    void printMonth(int year,int month)
    {
    int first,days,i;
    //cin>>year>>month;
    cout<<"----------------------------"<<endl;
    cout<<year<<"/"<<month<<endl;
    cout<<"----------------------------"<<endl;
    cout<<left<<setw(4)<<"Sun"<<setw(4)<<"Mon"<<setw(4)<<"Tue"<<setw(4)<<"Wed"<<setw(4)<<"Thr"<<setw(4)<<"Fri"<<setw(4)<<"Sat"<<endl;
    first=weekOfTheFirstDay(year,month);
    days=daysOfMonth(year,month);
    for(i=0;i<first;i++)
    {
    cout<<" ";
    }
    for(i=1;i<=days;i++)
    {

    cout<<left<<setw(4)<<i;

    if((i+first)%7==0)
    {
    cout<<endl;
    }
    }
    cout<<endl<<endl;

    }
    int main()
    {
    int year,m;
    cin>>year;
    for(int m=1;m<=12;m++)
    {
    printMonth(year,m);
    }
    }

  • 1