1 minute read

Day of the Programmer (Hacker Rank)

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 .

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String solve(int year, int[] days){
        int sum = 0;
        String result = "";
        for(Integer day : days){
            sum += day;
        }

        int february = 28;
        if(year < 1918){
            if(year%4 == 0){
                february = 29;
            }
        }else if(year > 1918){
            if( (year%400 == 0) || (year%4 ==0 && year%100 > 0)){
                february = 29;
            }
        }else if(year == 1918){
            february = 14 + 1;
        }

        sum = 256 - (sum + february);
        result = sum + "." + "09." + year;

        return result;
    }

    public static void main(String[] args) {
        int[] days = {31, 31, 30, 31, 30, 31, 31};
        Scanner in = new Scanner(System.in);
        int year = in.nextInt();
        String result = solve(year, days);
        System.out.println(result);
    }
}

행운의 77문제 프로젝트
  행운의 77문제 프로젝트는 한 달동안 알고리즘 문제 77개를 푸는 프로젝트입니다.