USACO Friday the Thirteenth

1. 蔡勒公式:

来自百度百科

w:星期; w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六

  c:世纪(前两位数)
  y:年(后两位数)
  m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
  d:日
  [ ]代表取整,即只要整数部分。
  下面以中华人民共和国成立100周年纪念日那天(2049年10月1日)来计算是星期几,过程如下:
  w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
  =49+[49/4]+[20/4]-2×20+[26×(10+1)/10]+1-1
  =49+[12.25]+5-40+[28.6]
  =49+12+5-40+28
  =54 (除以7余5)
  即2049年10月1日(100周年国庆)是星期五。
  再比如计算2006年4月4日,过程如下:
  w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
  =6+[6/4]+[20/4]-2*20+[26*(4+1)/10]+4-1
  =-12 (除以7余5,注意对负数的取模运算!)

2. 取余运算:

请参考c语言的取模运算 - 迎宾的专栏 - 博客频道 - CSDN.NET



/*
ID: dollarzhaole
PROG: friday
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;
int week[8];
int getweek(int c, int y, int m, int d)
{
    int w = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10  + d - 1;
    return w;
}

int main()
{
    ofstream fout ("friday.out");
    ifstream fin ("friday.in");
    int n, tmp, year, month, i ,j;
    memset(week, 0, sizeof(week));
    fin >> n;
    for (i = 0; i < n; i++)
        for (j = 1; j <= 12; j++)
        {
            year = i;
            month = j;
            if (j < 3)
            {
                year -= 1;
                month += 12;
            }
            tmp = getweek((1900 + year) / 100, (1900 + year) % 100, month, 13) % 7;
            if (tmp < 0)
                tmp += 7;
            week[tmp]++;
        }
    fout << week[6] << ' ';
    for (i = 0; i < 5; i++)
        fout << week[i] << ' ';
    fout << week[5] << endl;
    return 0;
}


原文地址:https://www.cnblogs.com/dollarzhaole/p/3188951.html