Library Fine Hackerrank problem solution

Library Fine Hackerrank

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 X (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 X (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 1000 hackos.
Input Format

The first line contains 3 space-separated integers denoting the respective day, month, and year on which the book was actually returned.
The second line contains 3 space-separated integers denoting the respectiveday, month, and year on which the book was expected to be returned (due date).

Constraints
1<=D<=31 1<=M<=12 1<=y<=3000 It is guaranteed that all date is in Gregorian Calendar date. Output FormatPrint a single integer denoting the library fine for the book received as input.Sample Input9 6 2015 6 6 2015 Sample Output45 ExplanationGiven the following return dates: Actual: Da=9, Ma=9, Ya=2015
Expected: De=6, Me=6, Ye=2015

Because Ye=Ya, we know it is less than a year late.
Because Me=a, we know it’s less than a month late.
Because Dea, we know that it was returned late (but still within the same month and year).

Per the library’s fee structure, we know that our fine will be 15 hackos X (#days late) . We then print the result of 15 X (9-6)=45 as our output.

Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int d1; 
    int m1; 
    int y1; 
    scanf("%d %d %d",&d1,&m1,&y1);
    int d2; 
    int m2; 
    int y2; 
    scanf("%d %d %d",&d2,&m2,&y2);
    if(y2==y1){
        if(m2==m1){
            if(d1<d2)
            printf("0");
            else
            printf("%d",15*(d1-d2));
        }
        else{
             if(m1<m2)
            printf("0");
            else
            printf("%d",500*(m1-m2));
        }
    }
    else{
         if(y1<y2)
            printf("0");
        else
        printf("%d",10000*(y1-y2));
    }
    return 0;
}
#include <iostream>
#include<vector>

using namespace std;
int main() {
    int returned[3];
    int expected[3];

    //Actual Return Date
    cin >> returned[0];   // D1
    cin >> returned[1];   // M1
    cin >> returned[2];   // Y1

    //Expected return date
    cin >> expected[0];   //D2
    cin >> expected[1];   //M2
    cin >> expected[2];   //Y2

    int fine = 0;

    // Returned within the current year
    if (expected[2] == returned[2]) {
        if (expected[1] < returned[1]) {
            // Returned 1+ months late in current year
            fine = (returned[1] - expected[1]) * 500;
        } else if ( (expected[1] == returned[1]) && (expected[0] < returned[0]) ) {
            // Returned 1+ days late within the current month
            fine = (returned[0] - expected[0]) * 15;
        } // Else it is implied that the book was returned early
    } else if (expected[2] < returned[2]) {
        // Returned 1+ years late
        fine = 10000;
    } // Else it is implied that the book was returned 1+ years early
    cout<<fine<<endl;
}
d1 = map(int, raw_input().split())
d2 = map(int, raw_input().split())
if d1[2] > d2[2]:
    print 10000
elif d1[2] < d2[2]:
    print 0
elif d1[1] > d2[1]:
    print 500 * abs(d1[1] - d2[1])
elif d1[1] < d2[1]:
    print 0
elif d1[0] > d2[0]:
    print 15 * abs(d1[0] - d2[0])
else:
    print 0
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int rtday = in.nextInt();
        int rtmonth = in.nextInt();
        int rtyear = in.nextInt();
        int dueday = in.nextInt();
        int duemonth = in.nextInt();
        int dueyear = in.nextInt();
        int fine = 0;
        if(rtyear > dueyear)
            fine = 10000;
        else if(rtyear == dueyear){
            if(rtmonth > duemonth)
                fine = 500 * (rtmonth - duemonth);
            else if(rtmonth == duemonth){
                if(rtday > dueday)
                    fine = 15 * (rtday - dueday);
            }
        }
        System.out.println(fine);
    }
}

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.