Sunday, 29 May 2016

The Time in Words Hacker Rank Problem Solution.


Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int hr,mn;
    map<int,string>sen;

    sen[1] = "one";
    sen[2] = "two";
    sen[3] = "three";
    sen[4] = "four";
    sen[5] = "five";
    sen[6] = "six";
    sen[7] = "seven";
    sen[8] = "eight";
    sen[9] = "nine";
    sen[10] = "ten";
    sen[11] = "eleven";
    sen[12] = "twelve";
    sen[13] = "thirteen";
    sen[14] = "fourteen";
    sen[15] = "quarter";
    sen[16] = "sixteen";
    sen[17] = "seventeen";
    sen[18] = "eighteen";
    sen[19] = "nineteen";
    sen[20] = "twenty";
    sen[21] = sen[20] + " "+ sen[1];
    sen[22] = sen[20] + " "+ sen[2];
    sen[23] = sen[20] + " "+ sen[3];
    sen[24] = sen[20] + " "+ sen[4];
    sen[25] = sen[20] + " "+ sen[5];
    sen[26] = sen[20] + " "+ sen[6];
    sen[27] = sen[20] + " "+ sen[7];
    sen[28] = sen[20] + " "+ sen[8];
    sen[29] = sen[20] + " "+ sen[9];
    sen[30] = "half";
    cin >> hr >> mn;
    string ans;
    if (mn == 0) {
        ans = sen[hr] + " o' clock";
    }
    else if (mn <= 30) {
        string tmp1 = ((mn) == 1 ? "" : "s");
        string tmp2 = (mn != 30 && mn != 15 ? " minute"+ tmp1 : "");
        ans = sen[mn] + tmp2 + " past "+ sen[hr];
    }
    else if (mn > 30) {
        string tmp1 = ((mn) == 1 ? "" : "s");
        ans = sen[60 - mn]+ (mn != 45 ? " minute" + tmp1 : "")+ " to " + sen[hr + 1];
    }
    cout << ans << endl;
    return 0;
}

Divisible Sum Pairs Hacker Rank Week of Code - 20 Problem Solution.


Solution:

#include<iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[110],k,n,cnt = 0;
    cin >> n >> k;
    for(int i = 0; i < n; i++)cin >> ar[i];
    
    for(int i = 0; i < n; i++){
        for(int j = i+1; j < n; j++){
            if((ar[i]+ar[j])%k == 0)cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}

Friday, 27 May 2016

Library Fine Hacker Rank Problem Solution.


Solution:

#include<iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
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;
}

Sunday, 8 May 2016

C And C++ Program to extract last two digits of a given year Problem Solution.


C Solution:

#include<stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int year,y;
    printf("Enter a year in for digits: ");
    scanf("%d",&year);

    y = year % 100;
    printf("Last two digits of year is: %02d",y);

    return 0;
}

C++ Solution:

#include<iostream>
#include<iomanip>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int year;
    cout << "Enter a year in for digits: ";
    cin >> year;

    cout << "Last two digits of year is: " << setfill('0') << setw(2) << year % 100 << endl;;

    return 0;
}