P1202 [USACO1.1]黑色星期五Friday the Thirteenth

题目描述

13号又是一个星期五。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400.

这里有一些你要知道的:

1、1900年1月1日是星期一.

2、4,6,11和9月有30天.其他月份除了2月都有31天.闰年2月有29天,平年2月有28天.

3、年份可以被4整除的为闰年(1992=4*498 所以 1992年是闰年,但是1990年不是闰年).

4、以上规则不适合于世纪年。可以被400整除的世纪年为闰年,否则为平年。所以,1700,1800,1900和2100年是平年,而2000年是闰年.

请不要调用现成的函数

请不要预先算好数据(就是叫不准打表)!

输入输出格式

输入格式:

一个正整数n.

输出格式:

输入输出样例

输入样例#1:
20
输出样例#1:
36 33 34 33 35 35 34

说明

题目翻译来自NOCOW。

USACO Training Section 1.1

#include <cstdio>
#include <ctype.h>
#include <string>
#include <iostream>
#include <cstring>
#include <math.h>
#include <vector>
#include <queue>
#include <cstdio>
#include <iostream>
#include <cstring> 
using namespace std;
int year,last=1,go,ans[8];
int mouth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int n;
void pass(int nowyear)
{    
    for(int i=1;i<=12;i++)
    {
        int t=0;
        if(i==2)
        {
            if(nowyear%100&&nowyear%4==0)
                t=1;else
            if(nowyear%400==0)    t=1;
        }
        go=(mouth[i]+t+last)%7;
        if(go==0)    last=7;else
        last=go;
        ans[last]++;
    }
}
int main()
{
    scanf("%d",&n);
    last=13%7;
    ans[last]++;
    for(int i=0;i<n;i++)
        pass(1900+i);
    ans[last]--;
    printf("%d ",ans[6]);
    printf("%d ",ans[7]);    
    for(int i=1;i<=5;i++)
        printf("%d ",ans[i]);
    return 0;
}
原文地址:https://www.cnblogs.com/CLGYPYJ/p/6970833.html