loading...

Daily HackerRank Challenge - Day 9

wingkwong profile image Wing-Kam ・3 min read

Daily HackerRank Challenge (30 Part Series)

1) Daily HackerRank Challenge - Day 0 2) Daily HackerRank Challenge - Day 1 3 ... 28 3) Daily HackerRank Challenge - Day 2 4) Daily HackerRank Challenge - Day 3 5) Daily HackerRank Challenge - Day 4 6) Daily HackerRank Challenge - Day 5 7) Daily HackerRank Challenge - Day 6 8) Daily HackerRank Challenge - Day 7 9) Daily HackerRank Challenge - Day 8 10) Daily HackerRank Challenge - Day 9 11) Daily HackerRank Challenge - Day 10 12) Daily HackerRank Challenge - Day 11 13) Daily HackerRank Challenge - Day 12 14) Daily HackerRank Challenge - Day 13 15) Daily HackerRank Challenge - Day 14 16) Daily HackerRank Challenge - Day 15 17) Daily HackerRank Challenge - Day 16 18) Daily HackerRank Challenge - Day 17 19) Daily HackerRank Challenge - Day 19 20) Daily HackerRank Challenge - Day 20 21) Daily HackerRank Challenge - Day 21 22) Daily HackerRank Challenge - Day 22 23) Daily HackerRank Challenge - Day 23 24) Daily HackerRank Challenge - Day 24 25) Daily HackerRank Challenge - Day 25 26) Daily HackerRank Challenge - Day 26 27) Daily HackerRank Challenge - Day 27 28) Daily HackerRank Challenge - Day 28 29) Daily HackerRank Challenge - Day 29 30) Daily HackerRank Challenge - Day 30

About

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.


Grading Students

HackerLand University has the following grading policy:

Every student receives a grade in the inclusive range from 0 to 100.
Any grade less than 40 is a failing grade.
Sam is a professor at the university and likes to round each student's grade according to these rules:

If the difference between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.
If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.
For example, grade=84 will be rounded to 85 but grade=29 will not be rounded because the rounding would result in a number that is less than 40.

Given the initial value of grade for each of Sam's n students, write code to automate the rounding process.

Sample Input

4
73
67
38
33

Sample Output

75
67
40
33

The question states that there is no rounding if the value of grade is less than 38. We can print the input directly for this case.

if(s>=38){
    // TODO
}
cout << s <<"\n";

If it is greater than or equal to 38, we need to find out the diff between the input and the next multiple of 5.

d=5-(s%5);

Check if the diff is less than 3, if so, add it to the input

if(d<3) s+=d;

Final Solution

int n,s,d;

int main()  
{ 
    FAST_INP;
    cin >> n;
    REP(i,n){
        cin >> s;
        if(s>=38){
            d=5-(s%5);
            if(d<3) s+=d;
        }
        cout << s <<"\n";
    }
    return 0; 
} 

The Time in Words

At minutes=0, use o' clock. For 1 <= minutes <= 30 , use past, and for 30 < minutes use to. Note the space between the apostrophe and clock in o' clock. Write a program which prints the time in words for the input given in the format described.

image

Sample Input

5
47

Sample Output

thirteen minutes to six

Since we need to convert the number to word, we can make a list for the convertion.

char nums[][64] = { "zero", "one", "two", "three", "four", 
                    "five", "six", "seven", "eight", "nine", 
                    "ten", "eleven", "twelve", "thirteen", 
                    "fourteen", "fifteen", "sixteen", "seventeen", 
                    "eighteen", "nineteen", "twenty", "twenty one", 
                    "twenty two", "twenty three", "twenty four", 
                    "twenty five", "twenty six", "twenty seven", 
                    "twenty eight", "twenty nine", 
                    }; 

We can just list out all the possible cases and select the corresponding numbers.

case 1: m==0
case 2: m==1
case 3: m==59
case 4: m==15
case 5: m==30
case 6: m==45
case 7: m<=30
case 8: m>30

Final Solution

int h,m;
char nums[][64] = { "zero", "one", "two", "three", "four", 
                    "five", "six", "seven", "eight", "nine", 
                    "ten", "eleven", "twelve", "thirteen", 
                    "fourteen", "fifteen", "sixteen", "seventeen", 
                    "eighteen", "nineteen", "twenty", "twenty one", 
                    "twenty two", "twenty three", "twenty four", 
                    "twenty five", "twenty six", "twenty seven", 
                    "twenty eight", "twenty nine", 
                    }; 

int main()  
{ 
    FAST_INP;
    cin >> h >> m;

    if (m == 0) 
        printf("%s o' clock\n", nums[h]); 

    else if (m == 1) 
        printf("one minute past %s\n", nums[h]); 

    else if (m == 59) 
        printf("one minute to %s\n", nums[(h % 12) + 1]); 

    else if (m == 15) 
        printf("quarter past %s\n", nums[h]); 

    else if (m == 30) 
        printf("half past %s\n", nums[h]); 

    else if (m == 45) 
        printf("quarter to %s\n", nums[(h % 12) + 1]); 

    else if (m <= 30) 
        printf("%s minutes past %s\n", nums[m], nums[h]); 

    else if (m > 30) 
        printf("%s minutes to %s\n", nums[60 - m], nums[(h % 12) + 1]); 

    return 0; 
} 

Complete Code

Check out the complete code via below link

Daily HackerRank Challenge (30 Part Series)

1) Daily HackerRank Challenge - Day 0 2) Daily HackerRank Challenge - Day 1 3 ... 28 3) Daily HackerRank Challenge - Day 2 4) Daily HackerRank Challenge - Day 3 5) Daily HackerRank Challenge - Day 4 6) Daily HackerRank Challenge - Day 5 7) Daily HackerRank Challenge - Day 6 8) Daily HackerRank Challenge - Day 7 9) Daily HackerRank Challenge - Day 8 10) Daily HackerRank Challenge - Day 9 11) Daily HackerRank Challenge - Day 10 12) Daily HackerRank Challenge - Day 11 13) Daily HackerRank Challenge - Day 12 14) Daily HackerRank Challenge - Day 13 15) Daily HackerRank Challenge - Day 14 16) Daily HackerRank Challenge - Day 15 17) Daily HackerRank Challenge - Day 16 18) Daily HackerRank Challenge - Day 17 19) Daily HackerRank Challenge - Day 19 20) Daily HackerRank Challenge - Day 20 21) Daily HackerRank Challenge - Day 21 22) Daily HackerRank Challenge - Day 22 23) Daily HackerRank Challenge - Day 23 24) Daily HackerRank Challenge - Day 24 25) Daily HackerRank Challenge - Day 25 26) Daily HackerRank Challenge - Day 26 27) Daily HackerRank Challenge - Day 27 28) Daily HackerRank Challenge - Day 28 29) Daily HackerRank Challenge - Day 29 30) Daily HackerRank Challenge - Day 30

Posted on by:

wingkwong profile

Wing-Kam

@wingkwong

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

Discussion

markdown guide