• + 0 comments

    Pure C:

    #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
     */
    unsigned long days_since_bce(int num_years, int num_months, int num_days)
    {
        unsigned long accum    =    0;
        
        //------------- year handling ---------------------------------------
        // days in normal years
        accum += num_years * 365;
        
        // days in leap years
        accum += (num_years / 4) - (num_years / 100) + (num_years / 400);
        
        //------------- month handling --------------------------------------
        // add the number of days of the _previous_ months
        // fallthroughs are intentional
        switch (num_months)
        {
            case 12:    accum += 30; 
            case 11:    accum += 31; 
            case 10:    accum += 30; 
            case 9:     accum += 31; 
            case 8:     accum += 31; 
            case 7:     accum += 30; 
            case 6:     accum += 31; 
            case 5:     accum += 30; 
            case 4:     accum += 31; 
            case 3:     accum += 28; 
            case 2:     accum += 31; 
        }
        
        //------------- day handling --------------------------------------
        accum += num_days;
        
        //------------- done ----------------------------------------------
        return accum;
    }
    
    
    //----------------------------------------------------------------------------------------------
    
    int main() {
        int y_returned;
        int m_returned;
        int d_returned;
    
        scanf("%d %d %d", &d_returned, &m_returned, &y_returned);
        unsigned long return_date = days_since_bce(y_returned, m_returned, d_returned);
    
        int y_due;
        int m_due;
        int d_due;
    
        scanf("%d %d %d", &d_due, &m_due, &y_due);
        unsigned long due_date = days_since_bce(y_due, m_due, d_due);
        
        long fine = (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 done
        printf("%ld", fine );
    
        return 0;
    }