QUESTION DESCRIPTION
Sachin visits his family doctor on a date given in the format yyyy:mm:dd. Sachin's doctor suggests him to take pills every alternate day starting from that day.
Sachin being a forgetful person are pretty sure won’t be able to remember the last day he took the medicine and would end up in taking the medicines on wrong days.
So he come up with the idea of taking medicine on the dates whose day is odd or even depending on whether dd is odd or even.
Now your task is to find the number of pills Sachin took on right time before messing up for the first time.
Note:
Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100.
The centurial years that are exactly divisible by 400 are still leap years.
For example, the year 1900 is not a leap year; the year 2000 is a leap year.
Input Format:
First line will contain T, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, in the format yyyy:mm:dd
Output Format:
For each testcase, output in a single line the required answer.
Constraints:
1≤T≤1000
1900≤yyyy≤2038
yyyy:mm:dd is a valid date
Solution :
#include <iostream>
using namespace std;
// Function to check if a year is a leap year
bool isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 != 0 || year % 400 == 0) {
return true;
}
}
return false;
}
// Function to calculate the number of days in a month
int daysInMonth(int month, int year) {
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year)) {
return 29;
}
return days[month];
}
// Function to calculate the number of days from January 1, 1900 to the given date
int daysFrom1900(int year, int month, int day) {
int totalDays = 0;
for (int y = 1900; y < year; ++y) {
totalDays += isLeapYear(y) ? 366 : 365;
}
for (int m = 1; m < month; ++m) {
totalDays += daysInMonth(m, year);
}
totalDays += day - 1;
return totalDays;
}
int main() {
int T;
cin >> T;
while (T--) {
int year, month, day;
char sep1, sep2;
cin >> year >> sep1 >> month >> sep2 >> day;
int totalDays = daysFrom1900(year, month, day);
int pillsTaken = (totalDays + 1) / 2; // Sachin takes pills on odd days
cout << pillsTaken << endl;
}
return 0;
}
Parsing the Input Date:
- We start by taking the date in the format
yyyy:mm:dd
. - For example, if the input is “2024:08:15,” we extract the year (2024), month (8), and day (15).
- We start by taking the date in the format
Calculating Days from 1900:
- We calculate the total number of days from January 1, 1900 to the given date.
- For instance, if the input date is “2024:08:15,” we find out how many days have passed since January 1, 1900.
Checking Leap Years:
- We need to know if the year is a leap year (which has an extra day in February).
- Leap years occur every 4 years, except for years divisible by 100 (unless they are also divisible by 400).
- For example, 2000 was a leap year, but 1900 was not.
Odd or Even Days:
- Sachin takes pills on alternate days.
- If the day (dd) is odd, he takes a pill; if it’s even, he skips a day.
- We calculate the total number of pills Sachin took on the right days before making a mistake.
Output:
- The final output is the number of pills taken correctly before Sachin’s first mistake.