USACO--friday

####Friday the Thirteenth####

简述
要求你统计从1900年1月1日(Monday)到指定的1900+N-1年12月31日,其中的每个月13号是星期几。
我设置了初值为1989年12月13日,这天是星期三,然后就可以和后面的年份一样的处理了。常数组就记下每个月对7取余的多的数,就是比上个月的星期多的数。然后就统计到对应的一年。

代码

#include <stdio.h>
#include <stdlib.h>
int ts[7];
int mon[12] = {3, 3, 0, 3, 2, 3, 2, 3, 3, 2, 3, 2};
int main(void)
{
    freopen("friday.in", "r", stdin);
    freopen("friday.out", "w", stdout);
    int i, j, n;
    int y = 1900, day = 3;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        int leap = 0;
        y =1900 + i;
        if((y % 4 == 0 && y % 100 != 0)||(y % 400 == 0))leap = 1;
        for(j = 0; j < 12; j++)
        {
            if(j == 2 && leap)day++;
            day += mon[j];
            day %= 7;
            ts[day]++;
        }
    }
    printf("%d", ts[6]);
    for(i = 0; i < 6; i++)
        printf(" %d", ts[i]);
    printf("\n");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章