10
\$\begingroup\$

This program is a clock that only displays minutes passed since midnight. This program is supposed to be really simple but because of some "aesthetic bug", I wrote quite a lot if-else statements that I think can be simplified.


Format Input

The first line will contain an integer T, the number of test cases. Each test case will contain an number N, the number displayed by the strange clock.

Format Output

For each test case, print “Case X: “ (X starts with 1) and then print what time it is in 24 hours HH:MM format.

Constraints

1 <= T <= 1000

0 <= N <= 1439

#include <stdio.h>

int main()
{
    int T;
    scanf("%d", &T);

    for (int i = 1; i <= T; i++) {

        int N;
        scanf("%d", &N);

        int hours = N / 60;
        int minutes = N % 60;

        printf("Case #%d: ", i);

        if (N < 10) {
            printf("00:0%d\n", N);
        } else if (N > 10 && N < 60) {
            printf("00:%d\n", N);
        } else if (N > 60 && N < 600) {
            if (minutes == 0) {
                printf("0%d:00\n", hours);
            } else if (minutes > 0 && minutes < 10) {
                printf("0%d:0%d\n", hours, minutes);
            } else {
                printf("0%d:%d\n", hours, minutes);
            }
        } else {
            if (minutes == 0) {
                printf("%d:00\n", hours);
            } else if (minutes > 0 && minutes < 10) {
                printf("%d:0%d\n", hours, minutes);
            } else {
                printf("%d:%d\n", hours, minutes);
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ As I understand it, N is the number the clock must display, I.e. when N=1439, the clock should display 14:39. You should divide by 100 and not 60. \$\endgroup\$ – Édouard Sep 2 at 9:32
  • \$\begingroup\$ @Édouard since a day has 1440 minutes, it's more likely that N is the number of minutes since midnight. This crucial information is missing from the instructions, which probably prepares the students to real-life situations, which are very similar. \$\endgroup\$ – Roland Illig Sep 2 at 18:57
  • \$\begingroup\$ Or, the instructions may have included the definition of the "strange clock", and Elvan just didn't post this part. \$\endgroup\$ – Roland Illig Sep 2 at 19:00
15
\$\begingroup\$

Following @Martin R's comment, I'll make my comment above a solution:

printf already supports what you're trying to achieve with your if-then-else jungle:

#include <stdio.h>

int main()
{
    int T;
    scanf("%d", &T);

    for (int i = 1; i <= T; i++) {

        int N;
        scanf("%d", &N);

        int hours = N / 60;
        int minutes = N % 60;

        printf("Case #%d: %02d:%02d\n", i, hours, minutes);
    }
}

You're also missing to check the return value of scanf. It is used to report errors in the input. What is the purpose of the first scanf?

Edit

From @chux's comment: you should validate the constraints of N after the input.

Edit

Note: additional variables should indeed have no influence on performance and are a good way to document meaning of your code, so please ignore my initial statement about "saving variables".

Old version was:

Also, I'd change the variable naming. Use minutes instead of N to communicate clearly what you're handling in the variable. You can then write the computation directly in the parameter list of printf, saving you two additional variables.

\$\endgroup\$
  • 6
    \$\begingroup\$ "saving you two additional variables" - in this case, I think the separated variables are actually a good idea. They shouldn't incur a performance drop, and they help to self-document the code. \$\endgroup\$ – Reinderien Sep 1 at 12:56
  • \$\begingroup\$ Thank you, added it. \$\endgroup\$ – Cornholio Sep 1 at 18:11
  • \$\begingroup\$ "Use minutes instead of N to communicate clearly what you're handling in the variable." The symbol N is explicitly defined in the problem statement. However, the word minutes could mean either N or the "minutes" field in the output. \$\endgroup\$ – JiK Sep 2 at 12:52
  • 1
    \$\begingroup\$ Note that with input like -61, Output of -01:00 might be preferred over this answer's "-01:-01". Yet with "0 <= N <= 1439" and "minutes passed since midnight", we might not worry about yesterday. \$\endgroup\$ – chux Sep 2 at 22:02
10
\$\begingroup\$

If your code is checked by an automated process, you will probably fail all test cases because your code prints a #, which was not asked for. You should better remove it. Just print "Case %d" instead of "Case #%d".

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.