4 / 4
Dec 2022

It’s my first test and it fails due to TLE. Here’s the code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>

int main(){

    long current_num = 0, flag = 0;
    char input[13];

    while(1){
        // Reading, then checking for newline
        fgets(input, 12, stdin);
    
        if(input[0] == '\n') break;

        // Conversion and test for success
        current_num = strtol(input, NULL, 0);
        if(errno) return -1;

        // The actual task
        if((current_num != 42) && (flag != 1))
             printf("%ld\n", current_num);
        else
            flag = 1;
    }

    return 0;
}

It works just fine on the sample input/output test. I know there’s some extra stuff here, but otherwise it appears to be without any serious quirks. Am I forgetting something really important? I ran tests in console, should I try debugging with a file?

  • created

    Dec '22
  • last reply

    Dec '22
  • 3

    replies

  • 465

    views

  • 2

    users

What if there’s no newline after the last number?

Why not simply exit after reading 42?

You’re right. Btw, SPOJ’s stdins end with an EOF, but that doesn’t matter because in fact, it’s easier to just stop accepting input after reading 42 altogether. Thank you, have a nice day!

It will depend on whoever created the problem and uploaded the data. In this case yes, in others no. It’s best to not make any assumptions. It seems input files in Windows format (i.e. CR LF line endings) cause some users problems.