zoukankan      html  css  js  c++  java
  • USACO . 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
     
     
     
     
        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. 这让我感觉整个人都不好了,莫非有计算技巧?弱渣想不出来,求大神指点。
        此题大意是对于输入 N,统计 1900 年 1 月 — 1900+N-1 年 12 月 的每个 13 日分别是星期几的次数。
        我的代码跟官方的核心差不多,一股自豪感油然而生...终于不是被吊打了...
     1 #include <iostream>
     2 #include <fstream>
     3 using namespace std;
     4 #define Native 0
     5 #if Native
     6     #define fin cin
     7     #define fout cout
     8 #else
     9     ifstream fin("friday.in");
    10     ofstream fout("friday.out");
    11 #endif
    12 inline bool isLeap(int y){return ((y%4==0&&y%100!=0)||(y%400==0));}
    13 int main(){
    14     int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    15     int res[7]={0};
    16     int N,week=6;/* Jan 13, 1900 is Saturday */
    17 
    18     fin>>N;N+=1900;
    19     for(int y=1900;y<N;y++){
    20         days[1]=isLeap(y)?29:28;/* February */
    21         for(int m=0;m<12;m++){
    22             res[week]++;
    23             week+=days[m];
    24             week%=7; /* 13th of next month */
    25         }
    26     }
    27     /* output the results, from Sat to Fri */
    28     fout<<res[6];
    29     for(int i=0;i<6;i++)
    30         fout<<' '<<res[i];
    31     fout<<endl;
    32     return 0;
    33 }
        不过我的输出有点麻烦,为了从 0 到 6 地表示星期天到星期六...官方就简单粗暴让 0 表示星期六,果然姜还是老的辣啊!
        官方的闰年判断长得跟国内主流也不一样,涨姿势了。
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <assert.h>
     5 
     6 int
     7 isleap(int y)
     8 {
     9     return y%4==0 && (y%100 != 0 || y%400 == 0);
    10 }
    11 
    12 int mtab[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    13 
    14 /* return length of month m in year y */
    15 int
    16 mlen(int y, int m)
    17 {
    18     if(m == 1)    /* february */
    19         return mtab[m]+isleap(y);
    20     else
    21         return mtab[m];
    22 }
    23 
    24 void
    25 main(void)
    26 {
    27     FILE *fin, *fout;
    28     int i, m, dow, n, y;
    29     int ndow[7];
    30 
    31     fin = fopen("friday.in", "r");
    32     fout = fopen("friday.out", "w");
    33     assert(fin != NULL && fout != NULL);
    34 
    35     fscanf(fin, "%d", &n);
    36 
    37     for(i=0; i<7; i++)
    38         ndow[i] = 0;
    39 
    40     dow = 0;    /* day of week: January 13, 1900 was a Saturday = 0 */
    41     for(y=1900; y<1900+n; y++) {
    42         for(m=0; m<12; m++) {
    43             ndow[dow]++;
    44             dow = (dow+mlen(y, m)) % 7;
    45         }
    46     }
    47 
    48     for(i=0; i<7; i++) {
    49         if(i)
    50             fprintf(fout, " ");
    51         fprintf(fout, "%d", ndow[i]);
    52     }
    53     fprintf(fout, "
    ");
    54 
    55     exit(0);
    56 }



  • 相关阅读:
    leetcode_question_67 Add Binary
    几种常用控件的使用方法
    JavaBean讲解 规范
    [置顶] JDK-CountDownLatch-实例、源码和模拟实现
    恋人分手后需要做的不是挽回而是二次吸引
    leetcode_question_70 Climbing Stairs
    偶然碰到的Win7 64位下CHM 的问题解决
    FTP中各文件目录的说明
    深入理解line-height与vertical-align(1)
    行内元素和块级元素
  • 原文地址:https://www.cnblogs.com/BlackStorm/p/4708178.html
Copyright © 2011-2022 走看看