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

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

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
  • Проголосовать: не нравится

»
10 лет назад, # |
  Проголосовать: нравится +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.

»
10 лет назад, # |
  Проголосовать: нравится +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).

  • »
    »
    10 лет назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    Don't lie to us, bash-like redirection also works in Windows, and, obviously, Mac, because it uses bash :)

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

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

  • »
    »
    10 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I join contest with friends and once I submited it faster than all of them.

  • »
    »
    10 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    There are a lot of problems where largest input is like 2 MB or even more.

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    It's not about how many ms this saves you. It eases your life alot when implementing. You don't have to manually input in the terminal. It gets you the input from the file, thus you see the answer in output file or stdout in no time!

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

in Java:

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

is it available for python?

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    import sys, os.path
    if(os.path.exists('input_file.txt')):
        sys.stdin = open("input_file.txt","r")
        sys.stdout = open("output_file.txt","w")
    
    
»
3 года назад, # |
  Проголосовать: нравится -11 Проголосовать: не нравится

is this available for java?