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();
}
USACO . Friday the Thirteenth的更多相关文章
- USACO Section 1.1-3 Friday the Thirteenth
Friday the Thirteenth 黑色星期五 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数. 给出N年的一个 ...
- USACO Section1.1 Friday the Thirteenth 解题报告
friday解题报告 —— icedream61 博客园(转载请注明出处) -------------------------------------------------------------- ...
- Friday the Thirteenth 黑色星期五 USACO 模拟 超级简单做法
1003: 1.1.3 Friday the Thirteenth 黑色星期五 时间限制: 1 Sec 内存限制: 128 MB提交: 8 解决: 8[提交] [状态] [讨论版] [命题人:外部 ...
- USACO Section 1.1 Friday the Thirteenth 解题报告
题目 题目描述 黑色星期五是否真的是一件不同寻常的事情?按理来说每个月的13号可能是星期一,或者是星期二...或者是星期天,但是黑色星期五的存在让我们不禁开始猜想,难道每个月的13号刚好是星期五的频率 ...
- USACO 1.1.3 Friday the Thirteenth 黑色星期五
Description 13号又是一个星期5.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至 ...
- USACO 1.2 Friday the Thirteenth
注意闰月的部分细节很多. /* ID:Starry21 LANG:C++ TASK:friday */ #include<iostream> #include<string> ...
- JZOJ.1002【USACO题库】1.1.3 Friday the Thirteenth黑色星期五
每日一博第一天! 保持你的决心 题目描述 13号又是星期五是一个不寻常的日子吗? 13号在星期五比在其他日少吗?为了回答这个问题,写一个程序来计算在n年里13 日落在星期一,星期二......星期日的 ...
- USACO Training Section 1.1黑色星期五Friday the Thirteenth
题目描述 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900+N- ...
- USACO Chapter 1 解题总结
USACO Chapter 1 解题总结 1.1.1 Your Ride Is Here 基本字符串操作,无压力. 1.1.2 Greedy Gift Givers 基础模拟题,弄明白题意,不怕麻烦, ...
随机推荐
- VS2010开发工具使用技巧<之简单讲解>
俗语云:工欲善其事必先利其器! 1.代码放大 效果:放大前----------------------------------------------------------------->放大 ...
- OpenNLP:驾驭文本,分词那些事
OpenNLP:驾驭文本,分词那些事 作者 白宁超 2016年3月27日19:55:03 摘要:字符串.字符数组以及其他文本表示的处理库构成大部分文本处理程序的基础.大部分语言都包括基本的处理库,这也 ...
- 从游戏脚本语言说起,剖析Mono所搭建的脚本基础
0x00 前言 在日常的工作中,我偶尔能遇到这样的问题:“为何游戏脚本在现在的游戏开发中变得不可或缺?”.那么这周我就写篇文章从游戏脚本聊起,分析一下游戏脚本因何出现,而mono又能提供怎样的脚本基础 ...
- MySQL的数据模型
MySQL的数据类型主要分为三大类: 数值型(Numeric Type) 日期与时间型(Date and Time Type) 字符串类型(String Type) 1. 数值 MySQL的数值类型按 ...
- Lua BehaviourTree 各节点说明
项目说明 本行为树的代码使用Lua编写,所有的内容也建立的Lua的基础语法之上 因为公司项目需求,需要一套Lua的行为树代码,所以尝试从饥荒中抽离了行为树相关的代码.绝大多数节点行为与饥荒中相同,不过 ...
- Xamarin for Visual Studio V3.11.431 于 2015.4.3-2015.4.17 最新发布(Win & Mac)
Beta Release: April 3 edited April 17 in Visual Studio Released versions: Windows Xamarin.VisualStud ...
- LINQ to SQL语句(10)之Insert
1.简单形式 说明:new一个对象,使用InsertOnSubmit方法将其加入到对应的集合中,使用SubmitChanges()提交到数据库. var newCustomer = new Custo ...
- Java Swing interview
http://www.careerride.com/Swing-AWT-Interview-Questions.aspx Swing interview questions and answers ...
- Mybatis学习(一)
1)先导入pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...
- [moka同学笔记]PHPexcel之excel导出和导入
原案例来自http://www.sucaihuo.com/有修改 1.目录结构(文件不用解释,应该都可以看得懂,直接看代码)