• 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

     1 /*
     2     ID: qhn9992
     3     PROG: friday
     4     LANG: C++
     5 */
     6 
     7 #include <iostream>
     8 #include <cstring>
     9 #include <cstdio>
    10 
    11 using namespace std;
    12 
    13 bool year(int x)
    14 {
    15     if((x%100!=0&&x%4==0)||(x%400==0))
    16         return true;
    17     else
    18         return false;
    19 }
    20 
    21 const int day_1[12]={13,44,72,103,133,164,194,225,256,286,317,347};
    22 const int day_2[12]={13,44,73,104,134,165,195,226,257,287,318,348};
    23 
    24 int a[7];
    25 
    26 int main()
    27 {
    28     freopen("friday.in","r",stdin);
    29     freopen("friday.out","w",stdout);
    30 
    31     int n;
    32     cin>>n;
    33     memset(a,0,sizeof(a));
    34     int sum=0;
    35     for(int i=1900;i<=1900+n-1;i++)
    36     {
    37         for(int j=0;j<12;j++)
    38         {
    39             if(year(i)==0)
    40                 a[(day_1[j]+sum%7)%7]++;
    41             else
    42                 a[(day_2[j]+sum%7)%7]++;
    43         }
    44         sum+=365;
    45         if(year(i))
    46             sum++;
    47     }
    48     printf("%d %d %d %d %d %d %d
    ",a[6],a[0],a[1],a[2],a[3],a[4],a[5]);
    49 
    50     return 0;
    51 }
  • 相关阅读:
    php navigat备份
    IBM技术俱乐部主席竞选
    IBM技术俱乐部主席竞选
    IBM技术俱乐部主席竞选
    IBM技术俱乐部主席竞选
    分治策略实验报告补充示例 汉诺塔实现
    分治策略实验报告补充示例 汉诺塔实现
    分治策略实验报告补充示例 汉诺塔实现
    分治策略实验报告补充示例 汉诺塔实现
    数字三角形问题 NOJ 1226
  • 原文地址:https://www.cnblogs.com/CKboss/p/3276886.html
Copyright © 2020-2023  润新知