Friday the Thirteenth

Friday the Thirteenth


题目: Friday the Thirteenth

简答:


/*
ID: lixianm1
PROG: friday
LANG: C++
*/
#include 
< fstream >
#include 
< iostream >
#include 
< vector >
#include 
< map >
#include 
< string >
#include 
< cassert >

inline 
bool  IsLeapYear(unsigned  int  nYear) { return  (( ! (nYear & 3 )) && ( 0 != nYear % 100 )) || ( 0 == nYear % 400 );}

int  main( int  argc,  char *  argv[]) 
{    
    
//////////////////////////////////////////////////////////////////////// //open the file
    std:: string  strInFile     =   " friday.in " ;
    std::
string  strOutFile     =   " friday.out " ;

    std::ifstream fin(strInFile.c_str());
    std::ofstream fout(strOutFile.c_str());

    
if  ( ! fin)
    {
        std::cout
<< " failed to open file for read " << std::endl;
        
return   1 ;
    }
    
if  ( ! fout)
    {
        std::cout
<< " failed to open file for write " << std::endl;
        fin.close();
        
return   1 ;
    }
    
//////////////////////////////////////////////////////////////////////// //read file and init all variables
    

    
int  N;
    fin
>> N;

    
//////////////////////////////////////////////////////////////////////// //process

    
static   int  month_day[] = { 0 31 28 31 30 31 30 31 31 30 31 30 31 };
    
static   int  month_day2[] = { 0 31 29 31 30 31 30 31 31 30 31 30 31 };

    
int  aWeekCount[ 7 ] = { 0 };
    unsigned 
int  nTotalDays =   0 ;

    
for  ( int  i = 0 ; i < N;  ++ i)
    {
        
for  ( int  nMonth = 1 ; nMonth < 13 ++ nMonth)
        {
            
++ aWeekCount[(nTotalDays + 12 ) % 7 ];
            
            
if  ( ! IsLeapYear( 1900 + i))
            {
                nTotalDays
+= month_day[nMonth];
            }
else
            {
                nTotalDays
+= month_day2[nMonth];
            }            
        }        
    }
    
    
//////////////////////////////////////////////////////////////////////// //write the process result
     for  ( int  i = 0 ; i < 6 ++ i)
    {
        fout
<< aWeekCount[(i + 5 ) % 7 ] << "   " ;
    }
    fout
<< aWeekCount[ 4 ] << std::endl;

    
//////////////////////////////////////////////////////////////////////// //end and exit
    fin.close();
    fout.close();

    
// system("pause");
     return   0 ;
}

你可能感兴趣的:(Friday the Thirteenth)