Compiling Java
Running Java
Compiling C/C++
Running C/C++
Using Standard Input and Output
Compiling Java is fairly simple, with a few things to keep an eye on.
The source file name must match the class name, including capitalization.
Example, if you have 'd.java', the source should look like:
public class d
{
public static void main(String Args[])
{
}
}
And if you had 'probA.java' then the source should look like:
public class probA
{
public static void main(String Args[])
{
}
}
To compile simply run:
javac src
So, for the examples above:
javac d.java
or
javac probA.java
If successful this should produce no output to the screen and create a class file.
java -cp . class
So, for the examples:
java -cp . d
or
java -cp . probA
Note1: this does not include the '.class' extension. You just tell it the
class to load.
Note2: the '-cp .' parameter tells java to use the current directory '.' as the
classpath. This just means it will try to find the class you are telling it to load
in the current directory. Many sites will have this in the class path already, but
adding the parameter won't hurt.
Lets suppose you have a.cpp, the command to compile it is:
gcc a.cpp -o a
This will create an executable named 'a'. This is the command the
judges will be using when they grade your problem. It also works
for .c files:
gcc a.c -o a
Now for some neat tricks:
If you compile with the parameter '-Wall' gcc will give you extra warnings.
These warnings are not always things that need to be fixed, they are just
things gcc finds suspicious and might indicate problems you need to fix.
Example:
gcc -Wall a.cpp -o a
This just saves typing. The 'make' command is known to many with unix/linux
experience. However, it is not so well known that it can perform common actions
with needing to create a 'Makefile'.
Lets again suppose you have a.cpp or a.c in the current directory.
The command 'make a' will detect which type of source you have and run
gcc with an appropriate command to generate the executable 'a'.
Note: depending on system configuration this method might include some
optimizations or architecture specific options in the gcc command. In most
cases this will not be a problem, but it might make your program run slightly
differently on your workstation that it does on the judge's.
Running C or C++ programs is simple. Once it is compiled into an executable,
you can execute it with a command like ./a
The './' tells your shell program to look in the current directory for
the executable.
All problems in this contest will use standard input and output.
Input data will come in on standard in and your program will be expected
to write output to standard out.
This means in Java, System.in and System.out and in C/C++,
cin and cout or scanf() and printf() depending on your preference.
When testing your programs, you can simply run the programs as described
above and type input into it. It should send the output to the screen.
Alternatively, you can create and input file and send that as standard in to
your program. This has the advantage of being faster, since you don't have
to type your input in for each test you do.
Lets suppose your working on problem A and have created an input file named 'a.in'.
You could execute your program with that input with a command like:
cat a.in | ./a
or
./a < a.in
Both of these commands do the same thing, which is take the contents of 'a.in'
and use that as the input to the program 'a'.
This author prefers the first example, since it reads logicly from left to right.
It basicly reads as, read file 'a.in' (cat), redirect the output of that command
to the input of the next command (|) and the next command is './a'.
Here is the same thing, but assuming
you did it in java and the class name is 'a'.
cat a.in | java -cp . a
or
java -cp . a < a.in
Any of these examples will send the output of your program to the screen.
If instead you wanted to send the output to a file, so you could for example
print it out and check it on paper that could be done with a command like:
cat a.in | ./a > a.out
This can be read as, read file 'a.in', redirect it to program a, send the output
to file 'a.out'.