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();
}

最新文章

  1. Java中六大时间类的使用和区别
  2. C#中使用Socket请求Web服务器过程
  3. JavaScript字符转Unicode,顺便说句:GitHub的Oh no页面很亮
  4. Android TextView走马灯效果
  5. Types of intraclass correlation coefficience (ICC)
  6. 第四章:管道与FIFO
  7. java 8-5 抽象
  8. 认识与学习BASH(中)
  9. vs2008 release下调试状态设置[转]
  10. 读改善c#代码157个建议:建议1~3
  11. css超出内容以省略号显示
  12. VUE-004-禁止修改页面显示项,设置el-input,textarea只读方法
  13. ZT Linux可用的最新版本的sublime text注册
  14. super和this的区别
  15. 软间隔分类——SVM
  16. 3.STM32复位系统
  17. PHP 中解析 url 并得到 url 参数
  18. Monkey测试执行_真机测试(2)
  19. cent os 6.5 配置vsftpd
  20. JAVA-JSP内置对象之request获得参数的参数值(一个值)

热门文章

  1. react+redux教程(五)异步、单一state树结构、componentWillReceiveProps
  2. springboot(八):RabbitMQ详解
  3. Hadoop入门学习笔记---part4
  4. 对C语言islower、isupper、isdigit函数的测试
  5. NSIS 打包脚本基础
  6. 利用Python进行数据分析(11) pandas基础: 层次化索引
  7. Django admin美化插件suit应用[原创]
  8. Razor语法中绑定一个值给checkbox
  9. DataGridView绑定源码下载
  10. python 添加tab补全