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 :)
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
forg++
. And just check it withMS 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.Thanks, I think this better than mine
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).Don't lie to us, bash-like redirection also works in Windows, and, obviously, Mac, because it uses bash :)
It's pretty useless, because fast in/out only needed in 0.001% of cases.
I join contest with friends and once I submited it faster than all of them.
There are a lot of problems where largest input is like 2 MB or even more.
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!
in Java:
is it available for python?
is this available for java?
https://codeforces.com/blog/entry/14118?#comment-297843