/*
ID: jackson21
PROG: friday
LANG: C++
*/
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
struct mdate;
bool judge_leap_year(const mdate date);

struct mdate
{
unsigned short year;
unsigned char month;
unsigned char day;
unsigned char week;


};

void increase(mdate& date)
{
date.week = (date.week)%7 + 1;
switch (date.month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
// 处理当天数增加时的影响
if ( date.day == 31 )
{
date.day = 1;
if(date.month == 12)
{
date.month = 1;
date.year++;
}
else
date.month++;
}
else
{
date.day++;
}
}
break;
case 4:
case 6:
case 9:
case 11:
{
// 处理每个月30天的情况
if ( date.day == 30 )
{
date.day = 1;
date.month++;
}
else
{
date.day++;
}
}
break;
case 2:
{
// first judge whether it is leap year
if ( date.day < 28 )
{
date.day++;
}
else if ( date.day == 28 )
{
if (!judge_leap_year(date))
{
date.month++;
date.day = 1;
}
else
date.day++;
}
else
{
date.day = 1;
date.month++;
}
}
}
}

// void PrintDate(const mdate date)
// {
// cout << (int)date.year <<"/"<< (int)date.month << "/" << (int)date.day <<" is WEEK " << (int)date.week << endl;
// }

int main()
{
// entry point
// get input number of year
unsigned short counter[8] = {0}; // 1 - 7 stands for Monday to Sunday 
ifstream fin;
ofstream fout;
unsigned short years = 0;
fin.open("friday.in");
fout.open("friday.out");
fin >> years;

mdate original = {1900, 1, 1, 1};
while(original.year <= (1900 + years - 1))
{
increase(original);
if ( original.day == 13 )
{
counter[original.week]++;
}
}

fout << counter[6] << " "<< counter[7] << " "<< counter[1] << " "<< counter[2] << " "
<< counter[3] << " "<< counter[4] << " "<< counter[5] << endl;

return 0;
}

bool judge_leap_year(mdate date)
{
if( date.year % 400 == 0 )
return true;
if( date.year % 4 == 0 )
{
if (date.year % 100 == 0)
return false;
else
return true;
}
else
{
return false;
}
}
 
关于atoi的应用,atoi把char字符串转换成int数值,这里有一个atoi的典型应用,即判断ip地址的合法性,需要把某个区域的char*转换成int判断范围,因此需要使用atoi函数,它的定义如下:int atoi ( const char * str );

unsigned short parse_ip(char* ip)
{
    // judge by length of ip address
    unsigned short len = strlen(ip);
    if (len < 7 || len > 15)
        return 0;

    char tmp[16] = {0};
    unsigned char i = 0;
    unsigned char j = 0;
    unsigned char dot_counter = 0;
    while(ip[j] != 0)
    {
        if (ip[j] >= '0' && ip[j] <= '9')
        {
            // digit
            tmp[i] = ip[j];
            if (strlen(tmp) > 3)
                return 0;  // 出现某个区域数字大于4位
            i++;
            j++;
            continue;
        }
        else if (ip[j] == '.')
        {
            // dot
if (i == 0)
return 0;
            dot_counter++;
unsigned short ai = atoi(tmp);
            if ( ai > 255 || ai < 0 )
                return 0;  // 某个区域数字不符合约束
            i = 0;
memset(tmp, 0, sizeof(tmp));
            j++;
            continue;
        }
        else
            return 0;      // 出现非法符号
    }

    if ( dot_counter != 3 )
        return 0;
if (atoi(tmp) > 255 || atoi(tmp) < 0)
return 0;
    return len;
}