USACO之Friday the Thirteenth

这道题只要模拟就行了,求出每个月13日是星期几,然后汇总,输出即可(注意输出顺序!)

求每个月13日是星期几,可以用递推的方法。首先先记录上个月13日是多少天,再计算从上个月13日到这个月13日有几天,这样这个月13日是星期几就很好求出了,而且速度很快。

废话少说,代码上:


#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
static int currentDay = -18;

void update(int month,int year)
{
    if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11 || month == 1)
    {
        currentDay += 31;
    }
    else if (month == 5 || month == 7 || month == 10 ||month == 12)
    {
        currentDay += 30;
    }
    else if (year % 4 == 0 && year % 100 != 0)
    {
        currentDay += 29;
    }
    else if (year % 400 == 0)
    {
        currentDay += 29;
    }
    else currentDay += 28;
}

int main(int argc, const char * argv[])
{
    int record[7];
    int years;
    int i,j;
    FILE * input;
    FILE * output;
    input = fopen("friday.in", "r");
    output = fopen("friday.out", "w");
    fscanf(input,"%d",&years);
    for (i = 0;i != 7;++i) record[i] = 0;
    for (i = 1900;i < 1900+years;++i)
    {
        for (j = 1;j != 13;++j)
        {
            update(j, i);
            record[currentDay % 7]++;
        }
    }
    fprintf(output,"%d",record[6]);
    for (i = 0 ;i != 6;++i)
    {
        fprintf(output," %d",record[i]);
    }
    fprintf(output, "\n");
    fclose(input);
    fclose(output);
    exit(0);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值