其他分享
首页 > 其他分享> > 1341. 十三号星期五

1341. 十三号星期五

作者:互联网

按天数模拟。

const int N=110;
int month[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int cnt[7];
int n;

bool isLeap(int year)
{
    return year % 4 == 0 && year % 100 || year % 400 == 0;
}

int main()
{
    cin>>n;

    int yy=1900,mm=1,dd=1;
    int weekday=0;
    while(yy < 1900+n-1 || mm < 12 || dd < 31)
    {
        dd++;
        weekday=(weekday+1)%7;
        if(dd == 13) cnt[weekday]++;
        
        if(dd == month[isLeap(yy)][mm]+1)
        {
            mm++;
            dd=1;
        }
        if(mm == 13)
        {
            yy++;
            mm=1;
        }
    }

    for(int i=0;i<7;i++)
        cout<<cnt[(i+5)%7]<<' ';

    //system("pause");
    return 0;
}

按月份模拟。

const int N=110;
int month[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int cnt[7];
int n;

bool isLeap(int year)
{
    return year % 4 == 0 && year % 100 || year % 400 == 0;
}

int main()
{
    cin>>n;

    int weekday=0;
    for(int i=0;i<n;i++)
    {
        int year=1900+i;
        for(int j=1;j<=12;j++)
        {
            cnt[(weekday+12)%7]++;
            weekday=(weekday+month[isLeap(year)][j])%7;
        }
    }
    
    for(int i=0;i<7;i++)
        cout<<cnt[(i+5)%7]<<' ';
    cout<<endl;
    //system("pause");
    return 0;
}

标签:mm,int,1341,十三号,30,星期五,dd,year,31
来源: https://www.cnblogs.com/fxh0707/p/14726665.html