USACO:Section 1.1 Friday the Thirteenth
/*ID:lwj13961 TASK:fridayLANG: C++*/#include<iostream>#include<fstream>#include<cassert>using namespace std;bool isleapyear(int year){if(year%100!=0&&year%4==0) return true;if(year%100==0&&year%400==0) return true;return false;}int main(){int N;int fre={0};int normalyear = {31,28,31,30,31,30,31,31,30,31,30,31};int leapyear = {31,29,31,30,31,30,31,31,30,31,30,31}; int *pyear; int days=0;//////////////////////////////////////get input char *ifs_file_name = "friday.in";/* inputfile name */ifstreamifs;/* create ifstream object */ifs.open (ifs_file_name);/* open ifstream */if (!ifs) {cerr << "\nERROR : failed to open inputfile " << ifs_file_name << endl;exit (EXIT_FAILURE);}ifs>>N;assert(N>=0&&N<=400);ifs.close ();/* close ifstream *///////////////////////////////////////for(int i=0;i<N;i++){if(isleapyear(1900+i)){pyear=leapyear;cout<<1900+i<<" 闰年 "; }elsepyear=normalyear;for(int j=0;j<12;j++){int t;if(j>0){days += pyear;t=(days+13)%7;}else{if(i>0)days += 31;t=(days+13)%7;}fre++;//cout<<1900+i<<"年 "<<j+1<<" 月13号是星期"<<t<<endl;}}//////////////////////////////////////make ouputchar *ofs_file_name = "friday.out";/* output file name */ofstreamofs;/* create ofstream object */ofs.open (ofs_file_name);/* open ofstream */if (!ofs) {cerr << "\nERROR : failed to open output file " << ofs_file_name << endl;exit (EXIT_FAILURE);}ofs<<fre;for(int i=0;i<6;i++)ofs<<" "<<fre; ofs<<endl;ofs.close ();/* close ofstream *///////////////////////////////////////getchar();getchar();return 0;}
页:
[1]