码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-04 11:06:39      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

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+简单模拟

标签:

原文地址:http://blog.csdn.net/acm_lkl/article/details/44057207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!