Posted on

Description

Submission

#include <bits/stdc++.h>

using namespace std;

// Complete the timeInWords function below.
string timeInWords(int h, int m) {
    string numbers[] = {"zero", "one", "two", "three", "four", "five", 
                        "six", "seven", "eight", "nine", "ten", 
                        "eleven", "twelve", "thirteen", "forteen", "quarter",
                        "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
                        "twenty one", "twenty two", "twenty three", "twenty four",
                        "twenty five", "twenty six", "twenty seven", "twenty eight", 
                        "twenty nine", "half"};
    
    if(m == 0) {
        return numbers[h] + " o' clock";
    }

    if(m == 1) {
        return "one minute past " + numbers[h];
    }
    else if(m <= 30) {
        return numbers[m] + (m == 15 || m == 30 ? "" : " minutes") 
                + " past " + numbers[h];
    } else {
        return numbers[60 - m] + (m == 45 ? "" : " minutes") + " to " + numbers[h+1];
    }
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    int h;
    cin >> h;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    int m;
    cin >> m;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    string result = timeInWords(h, m);

    fout << result << "\n";

    fout.close();

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *