Friday the Thirteenth

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.

Note that the start year is NINETEEN HUNDRED, not 1990.

There are few facts you need to know before you can solve this problem:

  • January 1, 1900 was on a Monday.
  • Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
  • Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
  • The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.

Do not use any built-in date functions in your computer language.

Don't just precompute the answers, either, please.

PROGRAM NAME: friday

INPUT FORMAT

One line with the integer N.

SAMPLE INPUT (file friday.in)

20

OUTPUT FORMAT

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34
 
 
 
 
    Brute force is a wonderful thing. 看,官方都说了,暴力出奇迹,可见此题水度。不过看官方继续解释:400 years is only 4800 months, so it is perfectly practical to just walk along every month of every year, calculating the day of week on which the 13th occurs for each, and incrementing a total counter. 这让我感觉整个人都不好了,莫非有计算技巧?弱渣想不出来,求大神指点。
    此题大意是对于输入 N,统计 1900 年 1 月 — 1900+N-1 年 12 月 的每个 13 日分别是星期几的次数。
    我的代码跟官方的核心差不多,一股自豪感油然而生...终于不是被吊打了...
 #include <iostream>
#include <fstream>
using namespace std;
#define Native 0
#if Native
#define fin cin
#define fout cout
#else
ifstream fin("friday.in");
ofstream fout("friday.out");
#endif
inline bool isLeap(int y){return ((y%==&&y%!=)||(y%==));}
int main(){
int days[]={,,,,,,,,,,,};
int res[]={};
int N,week=;/* Jan 13, 1900 is Saturday */ fin>>N;N+=;
for(int y=;y<N;y++){
days[]=isLeap(y)?:;/* February */
for(int m=;m<;m++){
res[week]++;
week+=days[m];
week%=; /* 13th of next month */
}
}
/* output the results, from Sat to Fri */
fout<<res[];
for(int i=;i<;i++)
fout<<' '<<res[i];
fout<<endl;
return ;
}

不过我的输出有点麻烦,为了从 0 到 6 地表示星期天到星期六...官方就简单粗暴让 0 表示星期六,果然姜还是老的辣啊!

    官方的闰年判断长得跟国内主流也不一样,涨姿势了。
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h> int
isleap(int y)
{
return y%== && (y% != || y% == );
} int mtab[] = { , , , , , , , , , , , }; /* return length of month m in year y */
int
mlen(int y, int m)
{
if(m == ) /* february */
return mtab[m]+isleap(y);
else
return mtab[m];
} void
main(void)
{
FILE *fin, *fout;
int i, m, dow, n, y;
int ndow[]; fin = fopen("friday.in", "r");
fout = fopen("friday.out", "w");
assert(fin != NULL && fout != NULL); fscanf(fin, "%d", &n); for(i=; i<; i++)
ndow[i] = ; dow = ; /* day of week: January 13, 1900 was a Saturday = 0 */
for(y=; y<+n; y++) {
for(m=; m<; m++) {
ndow[dow]++;
dow = (dow+mlen(y, m)) % ;
}
} for(i=; i<; i++) {
if(i)
fprintf(fout, " ");
fprintf(fout, "%d", ndow[i]);
}
fprintf(fout, "\n"); exit();
}

USACO . Friday the Thirteenth的更多相关文章

  1. USACO Section 1.1-3 Friday the Thirteenth

    Friday the Thirteenth 黑色星期五 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数. 给出N年的一个 ...

  2. USACO Section1.1 Friday the Thirteenth 解题报告

    friday解题报告 —— icedream61 博客园(转载请注明出处) -------------------------------------------------------------- ...

  3. Friday the Thirteenth 黑色星期五 USACO 模拟 超级简单做法

    1003: 1.1.3 Friday the Thirteenth 黑色星期五 时间限制: 1 Sec  内存限制: 128 MB提交: 8  解决: 8[提交] [状态] [讨论版] [命题人:外部 ...

  4. USACO Section 1.1 Friday the Thirteenth 解题报告

    题目 题目描述 黑色星期五是否真的是一件不同寻常的事情?按理来说每个月的13号可能是星期一,或者是星期二...或者是星期天,但是黑色星期五的存在让我们不禁开始猜想,难道每个月的13号刚好是星期五的频率 ...

  5. USACO 1.1.3 Friday the Thirteenth 黑色星期五

    Description 13号又是一个星期5.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至 ...

  6. USACO 1.2 Friday the Thirteenth

    注意闰月的部分细节很多. /* ID:Starry21 LANG:C++ TASK:friday */ #include<iostream> #include<string> ...

  7. JZOJ.1002【USACO题库】1.1.3 Friday the Thirteenth黑色星期五

    每日一博第一天! 保持你的决心 题目描述 13号又是星期五是一个不寻常的日子吗? 13号在星期五比在其他日少吗?为了回答这个问题,写一个程序来计算在n年里13 日落在星期一,星期二......星期日的 ...

  8. USACO Training Section 1.1黑色星期五Friday the Thirteenth

    题目描述 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900+N- ...

  9. USACO Chapter 1 解题总结

    USACO Chapter 1 解题总结 1.1.1 Your Ride Is Here 基本字符串操作,无压力. 1.1.2 Greedy Gift Givers 基础模拟题,弄明白题意,不怕麻烦, ...

随机推荐

  1. 说说面向服务的体系架构SOA

    序言 在.Net的世界中,一提及SOA,大家想到的应该是Web Service,WCF,还有人或许也会在.NET MVC中的Web API上做上标记,然后泛泛其谈! 的确,微软的这些技术也确实推动着面 ...

  2. ASP.NET MVC5+EF6+EasyUI 后台管理系统(22)-权限管理系统-模块导航制作

    系列目录 最近比较忙,系统难度独步增加,文章的发布速度明显比以前慢了. 由于我们已经跑通了整个系统,所有东西都回到了简单,接下来我们做模块制作也就是操作SysModule表. 首先我们来回顾一下之前的 ...

  3. Java异常总结

    异常就是在程序中可能要发生的未知错误,java机制中异常分为2大类:Exception和Error. 对异常的处理方式有2种,一是将异常通过关键字throws抛出,二是将异常进行try catch处理 ...

  4. JavaScript学习总结(四)——jQuery插件开发与发布

    jQuery插件就是以jQuery库为基础衍生出来的库,jQuery插件的好处是封装功能,提高了代码的复用性,加快了开发速度,现在网络上开源的jQuery插件非常多,随着版本的不停迭代越来越稳定好用, ...

  5. 由objC运行时所想到的。。。

    objC语言不仅仅有着面向对象的特点(封装,继承和多态),也拥有类似脚本语言的灵活(运行时),这让objC有着很多奇特的功能-可在运行时添加给类或对象添加方法,甚至可以添加类方法,甚至可以动态创建类. ...

  6. java笔记--理解java类加载器以及ClassLoader类

    类加载器概述: java类的加载是由虚拟机来完成的,虚拟机把描述类的Class文件加载到内存,并对数据进行校验,解析和初始化,最终形成能被java虚拟机直接使用的java类型,这就是虚拟机的类加载机制 ...

  7. 学习javascript数据结构(二)——链表

    前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...

  8. WCF学习系列汇总

    最近在学习WCF,打算把一整个系列的文章都”写“出来,包括理论和实践,这里的“写”是翻译,是国外的大牛写好的,我只是搬运工外加翻译.翻译的不好,大家请指正,谢谢了.如果觉得不错的话,也可以给我点赞,这 ...

  9. asp.net MVC 应用程序的生命周期

    下面这篇文章总结了 asp.net MVC 框架程序的生命周期.觉得写得不错,故转载一下. 转载自:http://www.cnblogs.com/yplong/p/5582576.html       ...

  10. mongo DB for C#

    (1)Download the MongoDB C#驱动. http://www.nuget.org/packages/mongocsharpdriver/. (2) Add Reference to ...