Skip to content

Commit 7061be0

Browse files
authored
Merge pull request ashutosh97#63 from murtaza1112/master
When to take medicine-Codechef
2 parents ff49956 + 29b13c8 commit 7061be0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
When to take medicine
2+
3+
All submissions for this problem are available.You visit a doctor on a date given in the format yyyy:mm:dd. Your doctor suggests you to take pills every alternate day starting from that day. You being a forgetful person are pretty sure won’t be able to remember the last day you took the medicine and would end up in taking the medicines on wrong days.
4+
5+
So you 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. Calculate the number of pills you took on right time before messing up for the first time.
6+
7+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Language-c++
2+
3+
4+
Solution-
5+
6+
#include <iostream>
7+
using namespace std;
8+
int get_year(long int year,int month,int day);
9+
int main()
10+
{
11+
// your code goes here
12+
int test;
13+
cin>>test;
14+
while(test!=0)
15+
{ int month,day;
16+
long int year;
17+
char a,b;
18+
cin>>year>>a>>month>>b>>day;
19+
int answer=get_year(year,month,day);
20+
cout<<answer<<endl;
21+
test--;
22+
23+
}
24+
return 0;
25+
}
26+
27+
int get_year(long int year,int month,int day)
28+
{ bool leap_year=false;
29+
int answer{0};
30+
31+
32+
if((year%4==0 && year%100!=0) ||year%400==0)
33+
leap_year=true;
34+
35+
for(;;)
36+
{
37+
bool odd=day%2;
38+
39+
if(month ==1 ||month ==3 ||month ==5 ||month ==7 ||month ==8 ||
40+
month ==10 ||month ==12)
41+
{
42+
answer+=((31-day)/2) + 1;
43+
break;
44+
45+
}else if(month ==4 ||month ==6 ||month ==9 ||month ==11)
46+
{
47+
answer+=((30-day)/2) + 1;
48+
49+
((odd)?day=1:day=2);
50+
month++;
51+
continue;
52+
}else
53+
{
54+
if(leap_year)
55+
{
56+
answer+=((29-day)/2) + 1;
57+
break;
58+
}else
59+
{
60+
answer+=((28-day)/2) + 1;
61+
62+
((odd)?day=1:day=2);
63+
month++;
64+
continue;
65+
}
66+
67+
}
68+
}
69+
return answer;
70+
}

0 commit comments

Comments
 (0)