Library Fine
Problem Statement :
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: 1. If the book is returned on or before the expected return date, no fine will be charged (i.e.: fine = 0). 2. 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). 3. 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). 4. 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 10000 Hackos. Example d1, m1, y1 = 14, 7, 2018 d2, m2, y2 = 5, 7, 2018 The first values are the return date and the second are the due date. The years are the same and the months are the same. The book is 14 - 5 = 9 days late. Return 9 * 5 = 135. Function Description Complete the libraryFine function in the editor below. libraryFine has the following parameter(s): d1, m1, y1: returned date day, month and year, each an integer d2, m2, y2: due date day, month and year, each an integer Returns int: the amount of the fine or 0 if there is none Input Format The first line contains 3 space-separated integers, d1, m1, y1, denoting the respective day, month and year on which the book was returned. The second line contains 3 space-separated integers, d2, m2, y2, denoting the respective day, month and year on which the book was due to be returned. Constraints 1 <= d1, d2 <= 31 1 <= m1, m2 <=12 1 <= y1, y2 <= 3000 It is guaranteed that the dates will be valid Gregorian calender dates.
Solution :
Solution in C :
C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int edd, emm, eyyyy, add, amm, ayyyy;
int fine =0;
scanf("%d%d%d%d%d%d", &add, &amm, &ayyyy, &edd, &emm, &eyyyy);
if(ayyyy < eyyyy){
fine = 0;
}
else if(ayyyy > eyyyy){
fine = 10000;
}
else if(ayyyy == eyyyy){
if(amm < emm){
fine = 0;
}
else if(amm > emm){
fine = 500 * (amm - emm);
}
else if(amm == emm){
if(add <= edd){
fine = 0;
}
else if(add > edd){
fine = 15 * (add - edd);
}
}
}
printf("%d", fine);
return 0;
}
C ++ :
#include<iostream>
using namespace std;
int main()
{
int actual[3],expected[3],i,j;
for(i=0;i<3;i++)
cin>>actual[i];
for(i=0;i<3;i++)
cin>>expected[i];
if(actual[2]-expected[2]<0)
cout<<0;
else if(actual[2]-expected[2]>0)
cout<<10000;
else if(actual[1]-expected[1]<0)
cout<<0;
else if(actual[1]-expected[1]>0)
cout<<500*(actual[1]-expected[1]);
else if(actual[0]-expected[0]>0)
cout<<15*(actual[0]-expected[0]);
else
cout<<0;
}
Java :
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int d2 = sc.nextInt(), m2 = sc.nextInt(), y2 = sc.nextInt();
int d1 = sc.nextInt(), m1 = sc.nextInt(), y1 = sc.nextInt();
if (y2 > y1) {
System.out.println(10000);
} else if (y2 < y1 || m2 < m1 || m2 == m1 && d2 <= d1) {
System.out.println(0);
} else if (m1 == m2) {
System.out.println(15 * (d2 - d1));
} else {
System.out.println(500 * (m2 - m1));
}
}
}
python 3 :
"""libraryfine.py"""
def main(N):
sum0 = 0
D = N[0][0]-N[1][0]
M = N[0][1]-N[1][1]
Y = N[0][2]-N[1][2]
if D > 0 and M == 0 and Y == 0:
sum0 = 15*(D)
elif M > 0 and Y == 0:
sum0 = 500*(M)
elif Y > 0:
sum0 = 10000*(Y)
print(sum0)
if __name__ == '__main__':
N = [list(map(int,input().split(" "))) for i in [1,2]]
main(N)
View More Similar Problems
Find the Running Median
The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.
View Solution →Minimum Average Waiting Time
Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h
View Solution →Merging Communities
People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w
View Solution →Components in a graph
There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu
View Solution →Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →