Programming Tips

Here are some useful code snippets for programming contests like IDI Open. Feel free to use these during the contest. Remember that copy/paste from electronic sources is not allowed, so we recommend you print these out and bring to the contest for re-typing.

Reading input

One way of dealing with input in a contest, is through the examplecode below. The methods readInt() and readString() returns the next int/String. If there are no more tokens on the line, it tokenizes the next line and returns the first int/String. The StringTokenizer (Java) is a class that takes one String as input to the constructor, and splits it into tokens/substrings (whitespace marks the place to split). You can call nextToken() on the StringTokenizer to get the next token. In C++ we simply use cin for input.

Java:

import java.io.*;
import java.util.*;

public class Example {
static BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
static StringTokenizer st = new StringTokenizer("");

public static void main(String[] args) throws Exception {
// Your code here.
}

// Read next input-token as string.
static String readString() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(stdin.readLine());
}
return st.nextToken();
}

// Read next input-token as integer.
static int readInt() throws Exception {
return Integer.parseInt(readString());
}

// Read next input-token as double.
static double readDouble() throws Exception {
return Double.parseDouble(readString());
}
}

C++:

#include <iostream>

using namespace std;

int main(int argc,  char** args){
// Your code here.
}

// Read next input-token as string.
string readString() {
string token;
cin >> token;
return token;
}

// Read next input-token as integer.
int readInt() {
int token;
cin >> token;
return token;
}

// Read next input-token as double.
double readDouble() {
double token;
cin >> token;
return token;
}

Formatting floating-point numbers

When dealing with floating-point numbers, you're often asked to print the result using a given precision, for example 3 digits.

Java:

One way of doing this in Java is using a class called DecimalFormat, which takes as an argument a string defining the format.

static DecimalFormat DF = new DecimalFormat("0.000",
new DecimalFormatSymbols(Locale.ENGLISH));

To print in another format, just change the string argument of the DecimalFormat constructor.

When we then want to print out the number, we call format() on the DecimalFormat. This method returns a string we print out using System.out.println().

double number = 3.14159226538979;
System.out.println(DF.format(number));

C++:

In C++, one way to achieve this is using the setiosflags and setprecision functions from the header iomanip before printing. Here's an example:

#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc,  char** args){
cout << setiosflags(ios::fixed) << setprecision(3);

double number = 3.14159226538979;

//This will print 3.142 (that is, "number" rounded to 3 decimals after comma) cout << number;
}

To change the number of decimals again, simply call setprecision again the same way as above, with the desired number of decimals.