Coding Paradise. Learn. Ask. Code. REPEAT.

Sunday 23 August 2015

'Library fine' from Hackerrank solution in C language

Problem Statement
The Head Librarian at a library wants you to make a program that calculates the fine for returning the book after the return date. You are given the actual and the expected return dates. Calculate the fine as follows:
  1. If the book is returned on or before the expected return date, no fine will be charged, in other words fine is 0.
  2. If the book is returned in the same month as the expected return date, Fine = 15 Hackos × Number of late days
  3. If the book is not returned in the same month but in the same year as the expected return date, Fine = 500 Hackos × Number of late months
  4. If the book is not returned in the same year, the fine is fixed at 10000 Hackos.
Input Format
You are given the actual and the expected return dates in D M Y format respectively. There are two lines of input. The first line contains the D M Y values for the actual return date and the next line contains the D M Y values for the expected return date.


Constraints
1D31
1M12
1Y3000
Output Format
Output a single value equal to the fine.
Sample Input
9 6 2015
6 6 2015
Sample Output
45
Explanation
Since the actual date is 3 days later than expected, fine is calculated as 15×3=45 Hackos.

Solution
ALGORITHM
To calculate the fine we need to compare the dates starting from the year. 
First we compare the years of the 2 dates. If the return year is less than the expected return year, then fine = 0. If the return year is greater than the expected return year, the fine is calculated as fine = 10000 * difference of the years. If the return year is same as the expected return year, we compare the months of the two dates. 
If the return month is less than the expected return month, then fine = 0. If the return month is greater than the expected return month, fine is calculated as fine = 500 * difference of the months. If the return month is same as the expected return month, we compare the day at which the book was supposed to be returned and the day at which the book was returned. If the book was returned before or on the expected return day, fine = 0. If book was returned after the expected return day but in the same month then fine = 15 * difference of the two dates.
PROGRAM


0 comments:

Post a Comment

Popular Posts

Powered by Blogger.