1.1.3 Friday the Thirteenth 黑色星期五

Description

13号又是一个星期5。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400. 这里有一些你要知道的: 1900年1月1日是星期一.4,6,11和9月有30天.其他月份除了2月都有31天.闰年2月有29天,平年2月有28天.年份可以被 4整除的为闰年(1992=4*498 所以 1992年是闰年,但是1990年不是闰年)以上规则不适合于世纪年.可以被400整除的世纪年为闰年,否则为平年.所以,1700,1800,1900 和2100年是平年,而2000年是闰年.请不要预先算好数据(就是叫不准打表 0。0)!

Input

一个正整数n.

Output

七个在一行且相分开的整数,它们代表13日是星期六,星期日,星期一...星期五的次数.

Sample Input

20

Sample Output

36 33 34 33 35 35 34 

思路:这是一道模拟题,定义一个数组记date=13时就进行++操作,首先我们还要考虑的是为什么res函数里面的k值和date值初值为1开始?答:因为我们进行到  i=month [flag] [i] -1 的时候k和date的值为0,所以我们需要在执行一次循环让他们初值为1。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>

typedef long long LL;
const long long INF = 0x3f3f3f3f;
const long long mod = 1e9+7;
const double PI = acos(-1.0);
const int dir4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
const int maxx = 100010;
using namespace std;
int day[10];
int month[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
int judge(int n)
{
    if((n%4==0&&n%100!=0)||(n%400==0))
        return 1;
    return 0;
}
int res(int flag,int n,int k)
{
    int date=1;
     for(int i=0;i<12;i++)
         for(int j=1;j<=month[flag][i];j++)
         {
             date++;
             date%=month[flag][i];
             k++;
             k%=7;
             if(date==13)
                 day[k]++;
         }
     return k;
}
int main()
{
    int n,k=1;
    cin>>n;
    for(int i=1900;i<=1900+n-1;i++)
    {
        k=res(judge(i),i,k);
    }
    cout<<day[6]<<" "<<day[0]<<" "<<day[1]<<" "<<day[2]<<" "<<day[3]<<" "<<day[4]<<" "<<day[5]<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38984851/article/details/82501579