题意:计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数
分析:
1. 用数组保存每个月的天数,外循环判断是否是闰年,若是闰年,二月天数为29。
2. 因为19001月13日是周六,用变量l 记录13日是周几,初始化为6,内循环 逐个加上每个月的天数,并通过模7得到是周几。
闰年的判断:

bool inline IsLeap(int year)
{
if((year%100!=0 && year%4==0)||year%400==0)
return true;
else
return false;
}

/**//*
ID: lorelei3
PROG: friday
LANG: C++
*/

#include <fstream>

using namespace std;

int day[7];

int mon[12] =
{31,28,31,30,31,30,31,31,30,31,30,31};


bool inline IsLeap(int year)
{
if((year%100!=0 && year%4==0)||year%400==0)
return true;
else
return false;
}


int main()
{
int n,i,j,l=6;
ifstream in("friday.in");
ofstream out("friday.out");

in>>n;
n+=1900;

for(i=1900; i<n; ++i)
{
if(IsLeap(i))
mon[1]=29;

for(j=0; j<12; ++j)
{
day[l]++;
l+=mon[j];
l%=7;
}
mon[1]=28;
}
out<<day[6];
for(i=0;i<6;++i)
out<<" "<<day[i];
out<<endl;

return 0;
}
PS: 蔡勒公式
posted on 2010-11-02 22:17
小阮 阅读(246)
评论(0) 编辑 收藏 引用 所属分类:
USACO