zoukankan      html  css  js  c++  java
  • 【USACO 1.1.3】黑色星期五

    【问题描述】

    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.

    【输出格式】

    (friday.out)

    七个在一行且相分开的整数,它们代表13日是星期六,星期日,星期一...星期五的次数..

    【分析】

    两种做法:1、直接模拟。2、蔡勒公式(不会的自己百度百科)。

     1 #include <cstdlib>
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <algorithm>
     7 using namespace std;
     8 int n,day[10];
     9 int main()
    10 {
    11     int i,j,w;
    12     //文件操作
    13     freopen("friday.in","r",stdin);
    14     freopen("friday.out","w",stdout);
    15     memset(day,0,sizeof(day));
    16     scanf("%d",&n);
    17     for(i=0;i<n;i++)
    18     for(j=1;j<=12;j++)
    19     {
    20         int y=i%100,c=(1900+i)/100,d=13,m=j;
    21         if (m>=3) 
    22         w=y+(int)((double)y/4)+(int)((double)c/(double)4)-2*c+(int)((double)26*(m+1)/(double)10)+d-1;
    23         else //判断m为1或m为2 
    24         {
    25              m+=12;
    26              y=(i-1)%100;while (y<0) y+=100;
    27              c=(1900+i-1)/100;d=13;
    28              w=y+(int)((double)y/4)+(int)((double)c/(double)4)-2*c+(int)((double)26*(m+1)/(double)10)+d-1;
    29         }
    30         while (w<0) w+=7;
    31         day[w%7]++;
    32     }
    33     printf("%d ",day[6]);
    34     for(i=0;i<6;i++) printf("%d ",day[i]);
    35     return 0;
    36 }
  • 相关阅读:
    chrome浏览器postman 插件安装
    使用poi解决导出excel内下拉框枚举项较多的问题
    Nginx(三)------nginx 反向代理
    webpack 内存溢出 Allocation failed
    Postman 安装及使用入门教程 (谷歌浏览器插件版)
    jquery 控制 video 视频播放和暂停
    百度编辑器ueditor 光标位置的坐标
    mkdocs 生成帮助文档
    js 日期 相关
    vue-cli3 第三版安装搭建项目
  • 原文地址:https://www.cnblogs.com/hoskey/p/3756040.html
Copyright © 2011-2022 走看看