1. 程式人生 > >P1202 [USACO1.1]黑色星期五Friday the Thirteenth

P1202 [USACO1.1]黑色星期五Friday the Thirteenth

section nth 否則 span 12月 == sin blog 世紀

題目描述

13號又是一個星期五。13號在星期五比在其他日子少嗎?為了回答這個問題,寫一個程序,要求計算每個月的十三號落在周一到周日的次數。給出N年的一個周期,要求計算1900年1月1日至1900+N-1年12月31日中十三號落在周一到周日的次數,N為正整數且不大於400.

這裏有一些你要知道的:

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年是閏年.

請不要調用現成的函數

請不要預先算好數據(就是叫不準打表)!

輸入輸出格式

輸入格式:

一個正整數n.

輸出格式:

輸入輸出樣例

輸入樣例#1:
20
輸出樣例#1:
36 33 34 33 35 35 34

說明

題目翻譯來自NOCOW。

USACO Training Section 1.1

#include <cstdio>
#include <ctype.h>
#include <string>
#include <iostream>
#include <cstring>
#include <math.h>
#include 
<vector> #include <queue> #include <cstdio> #include <iostream> #include <cstring> using namespace std; int year,last=1,go,ans[8]; int mouth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int n; void pass(int nowyear) { for(int i=1;i<=12;i++) { int t=0;
if(i==2) { if(nowyear%100&&nowyear%4==0) t=1;else if(nowyear%400==0) t=1; } go=(mouth[i]+t+last)%7; if(go==0) last=7;else last=go; ans[last]++; } } int main() { scanf("%d",&n); last=13%7; ans[last]++; for(int i=0;i<n;i++) pass(1900+i); ans[last]--; printf("%d ",ans[6]); printf("%d ",ans[7]); for(int i=1;i<=5;i++) printf("%d ",ans[i]); return 0; }

P1202 [USACO1.1]黑色星期五Friday the Thirteenth