본문 바로가기
1 Day 1 Algorithms

[2019.02.13] Day of the Programmer

by 곰돌찌 2019. 2. 13.

Problem


Marie invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of the Programmer (the  day of the year) during a year in the inclusive range from  to .

From  to , Russia's official calendar was the Julian calendar; since  they used the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in , when the next day after January  was February . This means that in , February  was the  day of the year in Russia.

In both calendar systems, February is the only month with a variable amount of days; it has  days during a leap year, and  days during all other years. In the Julian calendar, leap years are divisible by ; in the Gregorian calendar, leap years are either of the following:

  • Divisible by .
  • Divisible by  and not divisible by .

Given a year, , find the date of the  day of that year according to the official Russian calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is .

For example, the given .  is divisible by , so it is a leap year. The  day of a leap year after  is September 12, so the answer is .

Function Description

Complete the dayOfProgrammer function in the editor below. It should return a string representing the date of the  day of the year given.

dayOfProgrammer has the following parameter(s):

  • year: an integer

Input Format

A single integer denoting year .

Constraints

Output Format

Print the full date of Day of the Programmer during year  in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is .

Sample Input 0

2017

Sample Output 0

13.09.2017

Explanation 0

In the year , January has  days, February has  days, March has  days, April has  days, May has  days, June has  days, July has  days, and August has  days. When we sum the total number of days in the first eight months, we get . Day of the Programmer is the  day, so then calculate  to determine that it falls on day  of the  month (September). We then print the full date in the specified format, which is 13.09.2017.

Sample Input 1

2016

Sample Output 1

12.09.2016

Explanation 1

Year  is a leap year, so February has  days but all the other months have the same number of days as in . When we sum the total number of days in the first eight months, we get . Day of the Programmer is the  day, so then calculate  to determine that it falls on day  of the  month (September). We then print the full date in the specified format, which is 12.09.2016.

Sample Input 2

1800

Sample Output 2

12.09.1800

Explanation 2

Since 1800 is leap year. Day lies on 12 September.


How I solved the problem


# 조건이 Julian Calendar인지 Gregorian Calendar인지 1918년도 인지 체크

# 2월이 윤달인지 아닌지 체크 (Julian일때와 Gregorian일 때가 다름), 1918년도일 때는 1월 31일 다음날이 2월 14일임! 13일의 갭이 있음을 체크!!!

# 달을 더해주고 256번째 날의 월과 일을 체크해주기


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
 
public class Solution {
 
    // Complete the dayOfProgrammer function below.
    static String dayOfProgrammer(int year) {
        int[] daysOfMonth = {312831303130313130313031};
 
        //year is Julian or Gregorian or neither
        String calName = "";
 
        if (year >= 1700 && year <= 1917) {
            calName = "Julian";
        } else if (year == 1918) {
            calName = "Neither";
        } else {
            calName = "Gregorian";
        }
 
        //wheter year is leap year
        boolean isLeapYear = true;
 
        if (calName.equals("Julian")) {
            if (year % 4 == 0) {
                isLeapYear = true;
            } else {
                isLeapYear = false;
            }
        } else if (calName.equals("Gregorian")) {
            if ((year % 400 == 0|| (year % 4 == 0 && year % 100 != 0)) {
                isLeapYear = true;
            } else {
                isLeapYear = false;
            }
        }
 
        //days are added until days are less than 256
        int months = daysOfMonth.length;
        int days = 0;
        int resultMonth = 0;
        int resultDay = 0;
 
        for (int i = 0; i < months; i++) {
            //February
            if (i == 1) {
                if (year == 1918) {
                    days += daysOfMonth[i] - 13;
                } else {
                    if (isLeapYear) {
                        days += daysOfMonth[i] + 1;
                    } else {
                        days += daysOfMonth[i];
                    }
                }
            } else {
                days += daysOfMonth[i];
            }
 
            if (days > 256) {
                days -= daysOfMonth[i];
                resultMonth = i + 1;
                break;
            }
        }
 
        resultDay = 256 - days;
        String monthStr = "";
 
        if (resultMonth < 10) {
            monthStr = "0" + resultMonth;
        } else {
            monthStr = "" + resultMonth;
        }
        return resultDay + "." + monthStr + "." + year;
    }
 
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
 
        int year = Integer.parseInt(bufferedReader.readLine().trim());
 
        String result = dayOfProgrammer(year);
 
        bufferedWriter.write(result);
        bufferedWriter.newLine();
 
        bufferedReader.close();
        bufferedWriter.close();
    }
}
 
cs


[출처 : https://www.hackerrank.com ]


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.02.15] Drawing Book  (0) 2019.02.15
[2019.02.14] Bon Appetit  (0) 2019.02.14
[2019.02.12] Migratory Birds  (0) 2019.02.12
[2019.02.11] Divisible Sum Pairs  (0) 2019.02.11
[2019.02.08] Birthday Chocolate  (0) 2019.02.08

댓글