/**//*ID: sdjllyh1PROG: fridayLANG: C++TIME: 2008/9/8*///#include "stdafx.h"#include <iostream>#include <fstream>#include <string>using namespace std;//时间类class DATETIME{private: int m_year; int m_month; int m_day; int m_week;//0 is Sunday,5 is fridaypublic: //判断给定的年份是否是闰年 static bool IsLeapYear(int year) { bool retIs; if(year % 100 ==0 ) { if(year % 400 == 0) retIs = true; else retIs = false; } else { if(year % 4 ==0) retIs = true; else retIs = false; } return retIs; } public: //构造时间对象 DATETIME(int year, int month, int day) { m_year = year; m_month = month; m_day = day; m_week = ComputeWeek(); } //获得星期 int GetWeek() { return m_week; }private: //计算星期 int ComputeWeek() { return GetDaysFrom1900() % 7; } //计算当前时间于1900/1/1的天数差,如果当前为1900/1/1,则返回1 int GetDaysFrom1900() { int retDays = 0; for(int i=1900; i<m_year; i++) { if(!IsLeapYear(i)) retDays += 365; else retDays += 366; } for(int i=1; i<m_month; i++) { switch(i) { case 1:case 3:case 5:case 7:case 8:case 10:case 12: retDays += 31; break; case 4:case 6:case 9:case 11: retDays += 30; break; case 2: if(IsLeapYear(m_year)) retDays += 29; else retDays += 28; break; } } retDays += m_day; return retDays; } };//end class DATETIMEint n;//题目给出的年数int weekCount[7];//每个星期出现的次数void Init(){ ifstream fin ("friday.in"); fin>>n; for(int i=0; i<7; i++) weekCount[i] = 0; fin.close();}void Run(){ //统计每个13号的星期数 for(int i=1900; i<1900+n; i++) for(int j=1; j<=12; j++) { DATETIME date = DATETIME(i,j,13); int week = date.GetWeek(); weekCount[week]++; }}void Show(){ ofstream fout ("friday.out"); for(int i=0; i<7; i++) { if(i != 6) fout<<weekCount[(i+6) % 7]<<' '; else fout<<weekCount[(i+6) % 7]; } fout<<endl; fout.close();}int main() { Init(); Run(); Show(); return 0;} 转载于:https://www.cnblogs.com/SDJL/archive/2008/09/08/1286808.html