`

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 non-negative and will not exceed 400.

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


分析:
Brute force is a wonderful thing. 400 years is only 4800 months, so it is perfectly practical to just walk along every month of every year, calculating the day of week on which the 13th occurs for each, and incrementing a total counter.

此题因为输出用的是标准输出而贡献了一个WA,唉………………

/*
ID:xxfz014
PROG:friday
LANG:C++
*/
#include <iostream>
#include <fstream>
using namespace std;
int a[7];
bool isLeap(int year)
{//判断是否是润年
    if((year%4==0&&year%100!=0)||(year%400==0)) return true;
    return false;
}
int getDays(int month,int year)
{//求得第year年第month月一共有几天
    if(month==0)    return 0;
    if(month==9||month==4||month==6||month==11)     return 30;
    if(isLeap(year))
    {
        if(month==2)    return 29;
        else            return 31;
    }
    else
    {
        if(month==2)    return 28;
        else            return 31;
    }
}
int main()
{
    ifstream fin("friday.in");
    ofstream fout("friday.out");

    int n;
    fin>>n;
    int sum=0;
    for(int i=0;i<n;i++)
    {
        for(int j=1;j<=12;j++)
        {
            a[(sum%7+13)%7]++;
            sum+=getDays(j,i+1900);
        }
    }
    fout<<a[6]<<" "<<a[0];
    for(int i=1;i<=5;i++)   fout<<" "<<a[i];
    fout<<endl;
    fin.close();
    fout.close();
    return 0;
}

分享到:
评论

相关推荐

    USACO题目Friday the Thirteenth及代码解析

    《USACO题目"Friday the Thirteenth"的代码解析》 USACO(美国计算机奥林匹克竞赛)是一场面向全球中学生举行的编程竞赛,旨在提升参赛者的算法设计和编程能力。"Friday the Thirteenth"是其中一道挑战题,目标是...

    USACO官网93题fps格式 OJ题库

    3 [1.1] 黑色星期五Friday the Thirteenth 4 [1.1] 坏掉的项链 Broken Necklace 5 [1.2] 命名那个数字 Name That Number 6 [1.2] 挤牛奶Milking Cows 7 [1.2] 方块转换 Transformations 8 [1.2] 回文平方数 ...

    USACO题解(NOCOW整理版)1

    具体包括了几个不同难度级别的问题,如“Your Ride Is Here”,“Greedy Gift Givers”,“Friday the Thirteenth”和“Broken Necklace”。每个问题的解析都提供了算法思想和解决方案,有的通过简单的数学计算,有...

    USACO所有题目题解

    本文主要解析其中三个题目:“Your Ride Is Here (ride)”,“Greedy Gift Givers (gift1)”,以及“Friday the Thirteenth (friday)”。 1. **Your Ride Is Here (ride)**: 这是一个相对简单的问题,属于"ad hoc...

    第1章总结1

    同时,"Friday the Thirteenth"题目引入了模运算,"Broken Necklace"则涉及到数组的使用。 接着,1.2节重点是完整搜索,如"Milking Cows"中运用离散化技术,"Transformations"和"Name That Number"通过枚举解决,而...

    USACO英汉对照题目

    1.1.3 "Friday the Thirteenth" 可能需要理解日期和时间处理,以及可能的数学计算。 1.1.4 "Broken Necklace" 可能涉及字符串操作和链表处理。 1.2.1 "Milking Cows" 可能与数组和排序有关,可能是优化挤牛奶的效率...

    USACO题解整理版

    FridaytheThirteenth(friday)这题需要对日期和星期的计算有一定的理解。算法中通过数组记录每个月的天数,并考虑闰年,利用模运算求解每月13日对应的星期几。这个问题实际上运用了历法计算和模运算的知识。 ...

    usaco题目的副本1

    在"Friday the Thirteenth"中,我们可以使用`Counter`来统计每个月第13号出现的频率。 4. **链表操作优化**:"Broken Necklace"的解决方案提到,可以通过将两个项链合并成一条直线来简化问题,从而避免频繁的单个...

    USACO(Train)解题报告.doc

    ### Chapter 1 Section 1.2 - Friday the Thirteenth (friday) 此题涉及到日期计算。通过模拟计算,可以按月或按年计算第13号是星期几。对于小规模数据,可以逐月处理;对于大规模数据,可以以年为单位,考虑闰年的...

    USACO全部译题

    - 题目“FridaytheThirteenth”可能与日期和概率相关,而“BrokenNecklace”和“PalindromicSquares”涉及对称和数列的数学问题。这些问题要求参赛者对数学概念有较深的理解,并能够将其应用到编程中。 3. 动态规划...

    USACO chap1 题解

    #### FridaytheThirteenth(friday) - **题目概述**:题目要求计算在特定年份范围内每个月13日是星期五的次数。 - **解题策略**: - 利用模拟方法,根据1900年1月13日是星期六这一已知条件,逐月计算13日对应的...

    英语口语学习8000句

    3. **It's the thirteenth of August.**(今天是8月13日。) ### 二、询问星期 #### 常用表达方式: 1. **What day is it?**(今天是星期几?) - 直接询问今天是星期几。 2. **What's today's date?**(今天是几...

    北师大版初中英语第一册下(部分单词表)

    - 示例:The thirteenth floor is often considered unlucky. - 用法:序数词,表示顺序中的第十三个位置。 - **Halloween 万圣节前夕** - 示例:Children love dressing up for Halloween. - 用法:节日名称,...

Global site tag (gtag.js) - Google Analytics