C++ · problem solving

USACO 1.1.3 Friday the Thirteenth

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

using namespace std;

int getNumOfDays(int month, int year)
{
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
else if ( month == 2)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
return 29;
else
return 28;
}
else
{
if (year % 4 == 0)
return 29;
else
return 28;
}
}
else
return 31;
}
int main()
{
ifstream ifile(“friday.in”);
ofstream ofile(“friday.out”);

int day = 2; // moday
int month;
int year;
int numOfDays; // 28 days or 29 or 30 or 31
int counter[7] = {0}; // counter for days in the week
int n;
ifile >> n;

for (year = 1900; year < 1900 + n; year++)
{
for (month = 1; month <= 12; month++)
{
numOfDays = getNumOfDays(month, year);
for (int i = 1; i <= numOfDays; i++)
{
day++;
if (day > 6)
day = day % 7;

if (i == 13)
counter[day]++;
}
}
}
for (int i = 0; i < 7; i++) // sizeof(counter)/sizeof(counter[0])
{
if (i == 6)
ofile << counter[i];
else
ofile << counter[i] << ” “;
}
ofile << endl;
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s