Skip to content

Commit 086620b

Browse files
committed
19
1 parent 7118c5f commit 086620b

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

#19:Counting_Sundays.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
typedef unsigned long long ull;
5+
6+
bool isLeapYear(ull year)
7+
{
8+
return (!(year % 4) && ((year % 100) || !(year % 400)));
9+
}
10+
11+
short getDayOfWeek(ull year, short month, short day)
12+
{
13+
short x;
14+
15+
x = (14 - month)/12;
16+
year -= x;
17+
x = month + 12 * x - 2;
18+
return ((day + year + year / 4 - year / 100 + year / 400 + (31 * x) / 12) % 7);
19+
}
20+
21+
int main() {
22+
ull days, count, y[2];
23+
short q, M0, M1;
24+
short m[2], d[2];
25+
short daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
26+
27+
cin >> q;
28+
for (short i = 0; i < q; i++) {
29+
// input
30+
for (int j = 0; j < 2; j++) {
31+
cin >> y[j];
32+
cin >> m[j];
33+
cin >> d[j];
34+
}
35+
36+
// get starting day
37+
if (d[0] != 1)
38+
{
39+
m[0] = (m[0] + 1) % 13;
40+
y[0] += (!m[0]);
41+
m[0] += (!m[0]);
42+
}
43+
days = getDayOfWeek(y[0], m[0], 1);
44+
45+
// start counting
46+
count = 0;
47+
M0 = m[0];
48+
for (ull year = y[0]; year <= y[1]; year++)
49+
{
50+
daysPerMonth[2] = 28 + isLeapYear(year);
51+
M1 = 12 * (year != y[1]) + m[1] * (year == y[1]);
52+
for (short month = M0; month <= M1; month++)
53+
{
54+
if (!(days % 7))
55+
{
56+
count++;
57+
days = 0;
58+
}
59+
days += daysPerMonth[month];
60+
}
61+
M0 = 1;
62+
}
63+
cout << count << endl;
64+
}
65+
return 0;
66+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,6 @@ ___
8080
___
8181
## <a href=https://www.hackerrank.com/contests/projecteuler/challenges/euler018/problem?isFullScreen> Problem 18: </a> Maximum path sum I <sup>[Easy]</sup>
8282
Find the maximum sum of numbers that can be obtained by starting at the top of a triangle of numbers and moving to adjacent numbers on the row below, for a given triangle.
83+
___
84+
## <a href=https://www.hackerrank.com/contests/projecteuler/challenges/euler019/problem?isFullScreen> Problem 19: </a> Counting Sundays <sup>[Easy]</sup>
85+
Find the number of Sundays that fell on the first of the month between two dates(both inclusive)

0 commit comments

Comments
 (0)