We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
static string solve(int year) {
//Initialize DOY, month, and day ints for return string
int dayOfYear = 256;
int resultMonth = 0;
int resultDay = 0;
//intialize array to hold # of days in each month
int[] daysPerMonth = new int[13]{0,31,28,31,30,31,30,31,31,30,31,30,31};
//conditions to determine months in Feb (if not 28)
if(year == 1918){
daysPerMonth[2] -= 13;
}
else if((year < 1918 && year % 4 == 0) || (year > 1918 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))){
daysPerMonth[2]++;
}
//Loop thru DPM to find date for DOY by subtracting days in each month until DOY < DPM
for(int m = 0; m < daysPerMonth.Length; m++){
if(dayOfYear <= daysPerMonth[m]){
resultMonth = m;
if(m == 2 && year == 1918){
dayOfYear += 13;
}
resultDay = dayOfYear;
break;
}
else{
dayOfYear -= daysPerMonth[m];
}
}
DateTime resultDate = new DateTime(year, resultMonth, resultDay);
return String.Format(resultDate.ToString("dd.MM.yyyy"));
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day of the Programmer
You are viewing a single comment's thread. Return to all comments →
If we're going to be like that, then: