Блог пользователя kk_ducviet

Автор kk_ducviet, 12 лет назад, По-английски

Most coders read input from file and write out to another, but some online judge likes CodeForces use stdin. When solving problems in a contest, eveyone always want to minimum submission time, these little code may help:

C/C++

#ifndef ONLINE_JUDGE
freopen(fi, "r", stdin);
freopen(fo, "w", stdout);
#else
// online submission
#endif

Pascal

{$IFDEF ONLINE_JUDGE}
{online submission}
{$ELSE}
{read & write file}
{$ENDIF}   

Why? Because on the online judge, ONLINE_JUDGE is defined to be true in C/C++

Notice that it also works on UVa, some others too but I didn't test it :)

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
12 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

It's better to define your own preprocessor option, for example LOCAL_PROJECT (and use it in all online judges).

You can do it using -DLOCAL_PROJECT for g++. And just check it with

#ifdef LOCAL_PROJECT
  freopen("a.in","r",stdin);
  //freopen("a.out","w",stdout); // console output is better (in most cases)
#else
  // add i/o method of specific testing system
#endif

MS VS users: Project -> Options -> C\C++ -> Preprocessor options.

And to use console output for debugging: Use Ctrl+F5 or _getch(); under #ifdef #endif in the end of your program. In this case, #include <conio.h> must me under #ifdef #endif too. Don't forget to run your project in Release configuration sometimes, it's much faster than Debug, but debugger won't work properly.

»
12 лет назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

i usually read input from a file, but don't write output to a file (i use normal stdout).
during contests, all i do is change the command ./a.out to ./a.out < input.txt (works only on Linux-based OS, but similar addition can be made for other OS too).

»
12 лет назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

It's pretty useless, because fast in/out only needed in 0.001% of cases.

»
10 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

in Java:

if (System.getProperty("ONLINE_JUDGE") == null) {
  // Input is a file
} else {
  // Input is System.in
}
»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

is it available for python?

»
5 лет назад, скрыть # |
 
Проголосовать: нравится -11 Проголосовать: не нравится

is this available for java?