USACO . Friday the Thirteenth
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
SAMPLE INPUT (file friday.in)
OUTPUT FORMAT
SAMPLE OUTPUT (file friday.out)
#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();
}
最新文章
- Java中六大时间类的使用和区别
- C#中使用Socket请求Web服务器过程
- JavaScript字符转Unicode,顺便说句:GitHub的Oh no页面很亮
- Android TextView走马灯效果
- Types of intraclass correlation coefficience (ICC)
- 第四章:管道与FIFO
- java 8-5 抽象
- 认识与学习BASH(中)
- vs2008 release下调试状态设置[转]
- 读改善c#代码157个建议:建议1~3
- css超出内容以省略号显示
- VUE-004-禁止修改页面显示项,设置el-input,textarea只读方法
- ZT Linux可用的最新版本的sublime text注册
- super和this的区别
- 软间隔分类——SVM
- 3.STM32复位系统
- PHP 中解析 url 并得到 url 参数
- Monkey测试执行_真机测试(2)
- cent os 6.5 配置vsftpd
- JAVA-JSP内置对象之request获得参数的参数值(一个值)
热门文章
- react+redux教程(五)异步、单一state树结构、componentWillReceiveProps
- springboot(八):RabbitMQ详解
- Hadoop入门学习笔记---part4
- 对C语言islower、isupper、isdigit函数的测试
- NSIS 打包脚本基础
- 利用Python进行数据分析(11) pandas基础: 层次化索引
- Django admin美化插件suit应用[原创]
- Razor语法中绑定一个值给checkbox
- DataGridView绑定源码下载
- python 添加tab补全