Home > Old Blog Posts > USACO 1.1.3 Friday the Thirteenth

USACO 1.1.3 Friday the Thirteenth


最傻瓜式的方法:一天天往上加,是13号就统计,由于题目数据太小,这种方法可以过。

稍好一点的方法:一月月往上加,预存储各月天数 days[12],若当前月i13号是星期x,则下月就是星期(x+days[i]-1) mod 7

也可采用蔡勒公式,具体参见wikipeadia。

vijos上笨笨的提高组模拟赛第一题是这道题的加强版,可以拿来一做。

代码:

/*
ID: dementr1
PROG: friday
LANG: C++
*/
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin(“friday.in”);
ofstream fout(“friday.out”);
int date=1,month=1,year=1900,day[7]={},i,n,week=0;
cin>>n;
while(year!=1900+n-1||date!=31||month!=12)
{
date++;
week=(week+1)%7;
if(date==13) day[week]++;
if(date>30&&(month==4||month==6||month==9||month==11))
{
date=1;
month++;
}
else if(date>31&&(month==1||month==3||month==5
||month==7||month==8||month==10))
{
date=1;
month++;
}
else if    ((month==2)&&((date>28&&( (year%4!=0)||(year%4==0&&year%100==0&&year%400!=0) ))||
(date>29&&year%4==0&&(year%100!=0||year%400==0))))
{
date=1;
month++;
}

else if(date>31&&month==12)
{
date=1;
month=1;
year++;
}
}
for(i=5;i<11;i++) cout<<day[i%7]<<” “;
cout<<day[11%7]<<endl;
cin>>n;
return 0;
}

Categories: Old Blog Posts
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: