Library Fine HackerRank

Botman
ProgrammerCave
Published in
2 min readDec 20, 2019

Problem :

Your local library needs your help! Given the expected and actual return dates for a library book, create a program that calculates the fine (if any). The fee structure is as follows:

* If the book is returned on or before the expected return date, no fine will be charged (i.e.: fine = 0).
* If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, fine = 15 Hackos * (the number of days late).
* If the book is returned after the expected return month but still within the same calendar year as the expected return date, the fine = 500 Hackos * (the number of months late).
* If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000 Hackos.

Charges are based only on the least precise measure of lateness. For example, whether a book is due January 1, 2017 or December 31, 2017, if it is returned January 1, 2018, that is a year late and the fine would be 10,000 Hackos.

Read full problem : Library Fine

Solution :

Let integer variables return_day, return_month, return_year represents returned date and due_day, due_month and due_year represents expected return date of a book.

If return_year < due_year, then no fine will be charged.

If return_year == due_year, then we have to check for return_month and return_day since book is returned within the same calendar year.

If book is returned within the same calendar year but after expected return month ( due_month) i. e. (return_year == due_year && return_month > due_month) then fine is 500 Hackos * (return_month - due_month).

If the book is returned within the same calendar year and within the same calendar month but after the expected return day i. e. (return_year == due_year && return_month == due_month && return_day > due_day) then fine is 15 Hackos * (return_day - due_day).

If the book is returned after the expected return year i. e. (return_year > due_year) then fine is 10000 Hackos.

C++ Implementation on Programmercave

Other Competitive Programming Problems and Solutions

Sherlock and Squares
Circular Array Rotation

Originally published at https://programmercave.com/ on December 20, 2019.

--

--