See a problem in else if statement
Libraray Fine Hackerrank solution in C || Balanced Brackets Hackerrank solution in C
- Get link
- X
- Other Apps
Library Fine
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.: .
- If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, .
- If the book is returned after the expected return month but still within the same calendar year as the expected return date, the .
- If the book is returned after the calendar year in which it was expected, there is a fixed fine of .
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 .
Function Description
Complete the libraryFine function in the editor below. It must return an integer representing the fine due.
libraryFine has the following parameter(s):
- d1, m1, y1: returned date day, month and year
- d2, m2, y2: due date day, month and year
Input Format
The first line contains space-separated integers, , denoting the respective , , and on which the book was returned.
The second line contains space-separated integers, , denoting the respective , , and on which the book was due to be returned.
Constraints
Output Format
Print a single integer denoting the library fine for the book received as input.
Sample Input
9 6 2015
6 6 2015
Sample Output
45
Hackerrank solution in C++:
#include<iostream> using namespace std; int main(){ int d1,d2,m1,m2,y1,y2,fine = 0; cin >> d1 >> m1 >> y1 >> d2 >> m2 >> y2; if(d1 > d2 && m1 == m2 && y1 == y2)fine = 15 * (d1-d2); else if(m1 > m2 && y1 == y2)fine = 500 * (m1-m2); else if(y1 > y2)fine = 10000; cout << fine << endl; return 0; }
Balanced Brackets
A bracket is considered to be any one of the following characters: (
, )
, {
, }
, [
, or ]
.
Two brackets are considered to be a matched pair if the an opening bracket (i.e., (
, [
, or {
) occurs to the left of a closing bracket (i.e., )
, ]
, or }
) of the exact same type. There are three types of matched pairs of brackets: []
, {}
, and ()
.
A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For example, {[(])}
is not balanced because the contents in between {
and }
are not balanced. The pair of square brackets encloses a single, unbalanced opening bracket, (
, and the pair of parentheses encloses a single, unbalanced closing square bracket, ]
.
By this logic, we say a sequence of brackets is balanced if the following conditions are met:
- It contains no unmatched brackets.
- The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets.
Given strings of brackets, determine whether each sequence of brackets is balanced. If a string is balanced, return YES
. Otherwise, return NO
.
Function Description
Complete the function isBalanced in the editor below. It must return a string: YES
if the sequence is balanced or NO
if it is not.
isBalanced has the following parameter(s):
- s: a string of brackets
Input Format
The first line contains a single integer , the number of strings.
Each of the next lines contains a single string , a sequence of brackets.
Constraints
- , where is the length of the sequence.
- All chracters in the sequences ∈ { {, }, (, ), [, ] }.
Output Format
For each string, return YES
or NO
.
Sample Input
3
{[()]}
{[(])}
{{[[(())]]}}
Sample Output
YES
NO
YES
Solution in C++;
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <stack>
using namespace std;
string isBalanced(string s){
stack <char> st;
for(auto c:s){
switch (c){
case '(':
case '{':
case '[':
st.push(c);
break;
case '}':
if(st.empty() || st.top()!='{' )
return "NO";
st.pop();
break;
case ']':
if(st.empty() || st.top()!='[')
return "NO";
st.pop();
break;
case ')':
if(st.empty() || st.top()!='(')
return "NO";
st.pop();
break;
default: break;
}
}
return st.empty() ? "YES":"NO";
}
int main(){
int t;
cin >> t;
for(int a0 = 0; a0 < t; a0++){
string s;
cin >> s;
cout << isBalanced(s) << endl;
}
return 0;
}
- Get link
- X
- Other Apps
Comments
Post a Comment