USACO-Section1.1 Friday the Thirteenth [日期]

18 篇文章 0 订阅
1 篇文章 0 订阅

2017-05-20
题目大意:

给定数字N (1<=N<=400),计算1900年到1900+N-1年里,每个月的13号出现在周六、周日、周一、……周五的次数。并按顺序输出。

样例输入:

20

样例输出:

36 33 34 33 35 35 34

题解:

已知1900年1月1日是星期一,判断润年的方式是:
int isLeap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
接下来只要从1900年1月1号开始枚举到1900+N-1年的最后一天,把所有13号都记录下来放在result[]数组里即可。

代码:

/*
ID: zachery1
PROG: friday
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <cstring>
#define cin fin
#define cout fout
using namespace std;
ifstream fin("friday.in");
ofstream fout("friday.out");
int dayOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int result[7];
int N;

int main() {
    memset(result, 0, sizeof(result));
    cin >> N;
    int weekday = 1;
    for (int year = 1900; year <= 1900 + N - 1; year++) {
        int isLeap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
        dayOfMonth[2] += isLeap;
        for (int month = 1; month <= 12; month++) {
            for (int day = 1; day <= dayOfMonth[month]; day++) {
                if (day == 13) {
                    result[weekday] += 1;
                }
                weekday = (weekday + 1) % 7;
            }
        }
        dayOfMonth[2] -= isLeap;
    }
    cout << result[6] << " ";
    for (int i = 0; i < 5; i++) cout << result[i] << " ";
    cout << result[5] << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值