USACO 1.1 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

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
/*
ID: cxq_xia1
PROG: friday
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int N;
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int countday[7]={0};
bool isLeap(int x)
{
    if(((x%4==0)&&(x%100!=0))||((x%400)==0))
        return true;
    else
        return false;
}
int main()
{
    int year,day;
    freopen("friday.in","r",stdin);
    freopen("friday.out","w",stdout);
    while(cin >> N)
    {
        day=1;
        memset(countday,0,sizeof(countday));
        for(int i=0;i<N;i++)
        {
            year=1900+i;
            if(isLeap(year))
                month[1]=29;
            else
                month[1]=28;

            for(int j=0;j<12;j++)
            {
                countday[(day+13)%7]++;
                day+=month[j];
            }
        }
        for(int i=0;i<6;i++)
            cout << countday[i] <<" ";
        cout << countday[6] <<endl;
    }
    return 0;
}

  

时间: 2024-10-29 19:10:26

USACO 1.1 Friday the Thirteenth的相关文章

USACO Section1.1 Friday the Thirteenth 解题报告

friday解题报告 —— icedream61 博客园(转载请注明出处) ------------------------------------------------------------------------------------------------------------------------------------------------[题目] 据说曾经的黑色星期五是13号?本题让我们看看13号与星期五是否比与别的日子更有缘. 给出非负数n,请告诉我在“1900年1月1

USACO Section 1.1-3 Friday the Thirteenth

Friday the Thirteenth 黑色星期五 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数. 给出N年的一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400. 注意,开始今年是一千九百年,不是1990 这里有一些你要知道的: 1.1900年1月1日是星期一. 2.4,6,11和9月有30天.其他月份除了2月都有31天.闰年2月有29天,

USACO。s1.1.3Friday the Thirteenth(数组)

题意:计算n年里的12 * n个月里,每个月的13号落在周日到周六的次数. 思路&易错点:两个数组,一个数组1代表月份,一个数组2代表星期几,天数 % 7作为数组2的下标,自增.判断是否为闰年.某年的1月1号是周m,下一年的1月1号是周m + 1(闰年的话就m + 2,这个是开始时错了,找了几百年bug),就可以得到下一年的1月1号代表周几,继续mod. 代码如下: /* TASK:friday LANG:C++ ID:huibaochen */ #include <iostream>

usaco Friday the Thirteenth

/* ID: modengd1 PROG: friday LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> using namespace std; int days[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int N; int counter[7]; bool isleap(int x) { if((x%4)==0&&am

USACO Chapter 1 Section 1.1

USACO的题解和翻译已经很多了... 我只是把自己刷的代码保存一下. 1.PROB Your Ride Is Here 1 /* 2 ID:xiekeyi1 3 PROG:ride 4 LANG:C++ 5 */ 6 7 #include<bits/stdc++.h> 8 using namespace std ; 9 10 int main() 11 { 12 freopen("ride.in","r",stdin); 13 freopen(&quo

COGS 696. [IOI1996][USACO 2.3] 最长前缀

★   输入文件:prefix.in   输出文件:prefix.out   简单对比时间限制:1 s   内存限制:128 MB 描述 USACO 2.3.1 IOI96 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 P 中的元素可以通过串联(元素可以重复使用,相当于 Pascal 中的 “+” 运算符)组成一个序列 S ,那么我们认为序列 S 可以分解为 P 中的元素.元素不一定要全部出现(如下例中B

USACO prefix TrieTree + DP

/* ID:kevin_s1 PROG:prefix LANG:C++ */ #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <map> #include <set> #include <algorithm> #include <cstdlib>

【USACO 1.3.4】牛式

[題目描述 ] 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ---------- * * * * * * ---------- * * * * 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的"部分乘积",第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找出所有的牛式. [格式] INPUT FORMAT: (f

USACO Your Ride Is Here

[USACO]Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do,