USACO--1.1Friday the Thirteenth+简单模拟

n只有400,所以可以直接暴力模拟。
因为一个小问题卡了很久。。。。。。

代码如下:

/* ID: 15674811 LANG: C++ TASK: friday */

#include<iostream>
#include<cstdio>
#include<cstring>
#include<fstream>
using namespace std;

bool is_leap(int year)
{
    if(year%4==0&&year%100!=0)
        return 1;
    if(year%400==0)
        return 1;
    return 0;
}

int main()
{
    int day[15]={31,31,28,31,30,31,30,31,31,30,31,30,31};
    ofstream fout("friday.out");
    ifstream fin("friday.in");
    int n;
    int week[10];
    while(fin>>n)
    {
        int tmp=3;  //记录前面那个月的13号是星期几
        memset(week,0,sizeof(week));
        for(int year=1900;year<1900+n;year++)
        {
            for(int j=1;j<=12;j++)
            {
                int d;
                d=day[j-1];
                if(j==3&&is_leap(year))
                    d=29;
                int t=(d%7+tmp)-((d%7+tmp)/7)*7;
                if(t==0)
                    t+=7;
                week[t]++;
                tmp=t;
            }
        }
        fout<<week[6]<<" "<<week[7]<<" ";
        for(int i=1;i<5;i++)
            fout<<week[i]<<" ";
        fout<<week[5]<<endl;
    }
 return 0;
}

你可能感兴趣的:(USACO--1.1Friday the Thirteenth+简单模拟)