HACKER RANK Algorithms - Implementation - Library Fine - Solution.'C'
Algorithms - Implementation - Library Fine - Solution
Problem Statement
The Head Librarian at a library wants you to create a program that calculates the fine for returning a book after the return date. You are given the actual and the expected return dates. Calculate the fine as follows:
- If the book is returned on or before the expected return date, no fine will be charged. In other words, the fine is
0 . - If the book is returned in the same calendar month as the expected return date, the fine =
15 Hackos× the number of late days. - If the book is not returned in the same calendar month but in the same calendar year as the expected return date, the fine =
500 Hackos× the number of late months. - If the book is not returned in the same calendar year, the fine is fixed at
10000 Hackos.
Input
You are given the actual and the expected return dates inD M Y format on two separate lines. 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. Here's a sample:
You are given the actual and the expected return dates in
9 6 2015
6 6 2015
Constraints:
1≤D≤31
1≤M≤12
1≤Y≤3000
The given date is a valid date on a Gregorian calendar.
The given date is a valid date on a Gregorian calendar.
Output
Print a single value representing the fine.
The sample output for the above input would be45 .
Since the actual return date is3 days later than expected, the fine is calculated as 15×3=45 Hackos.
Print a single value representing the fine.
The sample output for the above input would be
Since the actual return date is
Solution:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int d,d1,m,m1,y,y1,n1=0,n2=0,n3=0,n=0;
scanf("%d%d%d",&d,&m,&y);
scanf("%d%d%d",&d1,&m1,&y1);
if(y1>y)
printf("%d",n);
else if(d1>=d && m1>=m && y1>=y) printf("%d",n);
else if(d1<d || m1<m || y1<y)
{
if(y1!=y)
{
n1=10000;
}
else if(m1<m && y1==y)
{
m=m-m1;
n2=500*m;
}
else if(d1<d && m1==m && y1==y)
{
n3=(d-d1)*15;
}
n=n1+n2+n3;
printf("%d",n);
}
return 0;
}
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int d,d1,m,m1,y,y1,n1=0,n2=0,n3=0,n=0; scanf("%d%d%d",&d,&m,&y); scanf("%d%d%d",&d1,&m1,&y1); if(y1>y) printf("%d",n); else if(d1>=d && m1>=m && y1>=y) printf("%d",n); else if(d1<d || m1<m || y1<y) { if(y1!=y) { n1=10000; } else if(m1<m && y1==y) { m=m-m1; n2=500*m; } else if(d1<d && m1==m && y1==y) { n3=(d-d1)*15; } n=n1+n2+n3; printf("%d",n); } return 0; }
Comments
Post a Comment