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.
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>//----------------------------------------------------------------------------------------------/*! * @brief given a date in yyyymmdd format, returns the number of days elapsed since 01-01-01 CE * @note assumes gregorian, no error checking is done since valid dates guaranteed by problem statement */unsignedlongdays_since_bce(intnum_years,intnum_months,intnum_days){unsignedlongaccum=0;//------------- year handling ---------------------------------------// days in normal yearsaccum+=num_years*365;// days in leap yearsaccum+=(num_years/4)-(num_years/100)+(num_years/400);//------------- month handling --------------------------------------// add the number of days of the _previous_ months// fallthroughs are intentionalswitch(num_months){case12:accum+=30;case11:accum+=31;case10:accum+=30;case9:accum+=31;case8:accum+=31;case7:accum+=30;case6:accum+=31;case5:accum+=30;case4:accum+=31;case3:accum+=28;case2:accum+=31;}//------------- day handling --------------------------------------accum+=num_days;//------------- done ----------------------------------------------returnaccum;}//----------------------------------------------------------------------------------------------intmain(){inty_returned;intm_returned;intd_returned;scanf("%d %d %d",&d_returned,&m_returned,&y_returned);unsignedlongreturn_date=days_since_bce(y_returned,m_returned,d_returned);inty_due;intm_due;intd_due;scanf("%d %d %d",&d_due,&m_due,&y_due);unsignedlongdue_date=days_since_bce(y_due,m_due,d_due);longfine=(return_date-due_date)*15L;// returned early ?if(fine<0)fine=0;// returned after a year boundary ?if(y_returned>y_due)fine=10000;// returned after a month boundary ?if(y_returned==y_due){if(m_returned>m_due)fine=(500*(m_returned-m_due));}// all doneprintf("%ld",fine);return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 26: Nested Logic
You are viewing a single comment's thread. Return to all comments →
Pure C: