USACO-Section1.1 Friday the Thirteenth【泰勒公式】
来源:互联网 发布:股票医生软件下载 编辑:程序博客网 时间:2024/05/21 22:28
题目描述:
13号又是一个星期五。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400.
注意,开始今年是1900年,不是1990年.
这里有一些你要知道的:
1、1900年1月1日是星期一.
2、4,6,11和9月有30天.其他月份除了2月都有31天.闰年2月有29天,平年2月有28天.
3、年份可以被4整除的为闰年(1992=4*498 所以 1992年是闰年,但是1990年不是闰年).
4、以上规则不适合于世纪年。可以被400整除的世纪年为闰年,否则为平年。所以,1700,1800,1900和2100年是平年,而2000年是闰年.
请不要调用现成的函数.
请不要预先算好数据(打表).(翻译来源:NOCOW)
INPUT FORMAT:
一个正整数n.
OUTPUT FORMAT:
七个在一行且相分开的整数,它们代表13日是星期六,星期日,星期一…星期五的次数.
样例输入
20
样例输出
36 33 34 33 35 35 34
题目思路:
这道题的初衷应该是用判断闰年加枚举搜索来解决,不过我为了图省事用了泰勒公式。泰勒公式是判断周几的很好的方法,可以节省大量的时间,建议背下来~下面是代码。
#include <stdio.h>#include <stdlib.h>#include <string.h>int y,m,d,w;int s[7];int main () { FILE *fin = fopen ("friday.in", "r"); FILE *fout = fopen ("friday.out", "w"); int i,n; fscanf (fin, "%d", &n); int year =1900; for(i=1;year<1900+n;i++){ if(i>12){//如果超过12月,换算到1月,年加1 i=1; year++; if(year>=1900+n)//超过就break,否则会多一次计算 break; } if(i<3){//泰勒公式中如果月份小于3,则将之加12,年份减一,具体原因我也不懂~ m=i+12; y=year-1; } else{ m=i; y=year; } d=13;//第十三天 w=(y+y/4+y/400-y/100+m*2+3*(m+1)/5+d)%7;//泰勒公式!(w=6代表周日,注意!) s[w]++; } fprintf(fout,"%d %d %d %d %d %d %d\n",s[5],s[6],s[0],s[1],s[2],s[3],s[4]); exit(0);}
阅读全文
0 0
- USACO-Section1.1 Friday the Thirteenth【泰勒公式】
- USACO-Section1.1 Friday the Thirteenth [日期]
- USACO-Section1.1 Friday the Thirteenth [日期]
- USACO-Section1.1 Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth[日期]
- USACO Section1.1 Friday the Thirteenth
- USACO之Section1.1.3 Friday the Thirteenth
- Friday the Thirteenth-section1.1
- USCAO-Section1.1 Friday the Thirteenth
- Friday the Thirteenth(USACO)
- usaco--Friday the Thirteenth
- [USACO] Friday the Thirteenth
- usaco Friday the Thirteenth
- USACO Friday the Thirteenth
- USACO:Friday the Thirteenth
- USACO -Friday the Thirteenth
- Jqeury(操作HTML)
- Android AlertDialog的基本用法
- VCS-5 LVM和VxVM
- 11.RecyclerView使用
- ubuntu cannot access network using bridge to host windwos
- USACO-Section1.1 Friday the Thirteenth【泰勒公式】
- Linux常用命令介绍(搜索命令)
- Java反射概念
- 傲娇与偏见
- jQuery笔记
- 两数组的交
- JavaScript学习记录(4)
- spark报错:ERROR SparkUI: Failed to bind SparkUI,解决办法
- 图片无缝滚动