How to write your source code

HOWTOs

Moderator: Board moderators

Post Reply
enjolras
System administrator
Posts: 35
Joined: Mon Jul 19, 2004 5:13 pm

How to write your source code

Post by enjolras »

How to write your source code

The rules for writing programs are similar to contest rules. A brief resume: the program reads the test inputs from the standard input and places the results in the standard output; they don't need (in fact, they aren't allowed to) open files or to execute some system calls. Remember: never try to open any file or your program will be judged as wrong.

NOTE: To help some people, the Online Judge defines always the 'ONLINE_JUDGE' symbol while compiling your program. Thus, you can test for it to redirect the input/output to a file except while the Online Judge is testing your program. The following C example reads the input from a file called myprog.in and writes the output to myprog.out using only scanf() and printf(). The Online Judge will skip the open() and close() functions and will automatically use stdin and stdout instead of the files:

Code: Select all

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
                
main()
{
  int number;

#ifndef ONLINE_JUDGE
  close (0); open ("myprog.in", O_RDONLY);
  close (1); open ("myprog.out", O_WRONLY | O_CREAT, 0600);
#endif

  scanf  ("%d", &number);
  printf ("%d\n", number*number);
}
Please remember some limits: source code cannot be bigger than 40K and you shouldn't use more than 1000 characters per line.[/code]
Post Reply

Return to “HOWTOs”