-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountDate.cpp
More file actions
58 lines (45 loc) · 1.02 KB
/
countDate.cpp
File metadata and controls
58 lines (45 loc) · 1.02 KB
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
56
57
58
const int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct date
{
int year, month, day;
};
int leapyear(int year) //윤년이면 1 평년이면 0
{
if(!(year%400)) return 1;
if(!(year%100)) return 0;
if(!(year%4)) return 1;
return 0;
}
int countDate(struct date day)
{
int sum = 0;
//년도에 따른 날짜
sum += (day.year - 1)*365 + (day.year - 1)/4 - (day.year - 1)/100 + (day.year - 1)/400;
//달에 따른 날짜
for(int i = 1; i < day.month; i++) sum+=month[i];
if(day.month > 2) sum+=leapyear(day.year);
//일에 따른 날짜
sum+=(day.day);
return sum; //1년 1월 1일 = 1
}
date itoday(int n)
{
struct date day;
//년도 처리
for(int i = 1; n >= 365 + leapyear(i); i++)
{
n-=(365+leapyear(i));
day.year++;
}
//월 처리
for(int i = 1; n > month[i]; i++)
{
n-=month[i];
day.month++;
}
//윤년처리
if(day.month > 2) n-=leapyear(day.year);
//일 처리
day.day = n;
return day;
}