-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day of the year.cpp
55 lines (51 loc) · 1.3 KB
/
Day of the year.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
int dayOfYear(string date) {
string s;
string leap;
string str;
int a,x;
for(int i=0;i<=3;i++){
leap.push_back(date[i]);
}
int y;
y=stoi(leap);
if((y%4==0 && y%100!=0)|| y%400==0){
a=29;
}
else{
a=28;
}
int i=0;
int j=1;
int arr[12]={0,31,28,31,30,31,30,31,31,30,31,30};
for(int i=0;i<j;i++){
if((date[5]=='0'||date[5]=='1') && (date[6]>='0' && date[6]<='9')){
if(date[5]=='0'){
str.push_back(date[6]);
}
else{
str.push_back(date[5]);
str.push_back(date[6]);
}
if(date[8]=='0'){
s.push_back(date[9]);
}
else{
s.push_back(date[8]);
s.push_back(date[9]);
}
x=stoi(s);
int b=stoi(str);
if(a==29){
arr[2]=29;
}
while(b--){
x=x+arr[i];
i++;j++;
}
}
}
return x;
}
};