Programming Competition Tips

From ProgSoc Wiki

Jump to: navigation, search

Contents

1. How to compile and run your program:

Java

$ javac problem1.java
$ java problem1

N.B. the name of your class should also be problem1, with a single method:

public class problem1
{
        public static void main(String args[]) {
            // code
        }
}

C

$ gcc -lm problem1.c -o problem1
$ ./problem1

C++

$ g++ -lm problem1.cpp -o problem1
$ ./problem1

-lm links in the math library that contains various math related functions. You will still need to include the appropriate header files e.g. math.h. Java has no command line options for math functions, but you will need to import the appropriate class e.g. java.lang.Math.

Python

$ python problem1.py

or

$ python3 problem1.py

2. How to parse standard input in C:

How to read in an undetermined number of lines from standard input:

Basically, you set up a while loop that tests the result returned by the function used for input (scanf, fgets, sscanf, etc.), and terminate the loop if the result returned is zero or EOF (end of file).

The simplest is scanf, which is useful if each line of input is of known fixed format and length, e.g.

/* read 3 integers from each line of standard input, and print the sum for each line */
#include <stdio.h>
int main() {
int a,b,c;
  while (scanf("%d %d %d",&a,&b,&c)!=EOF) {
	printf("%d\n",a+b+c);
  }
  return 0;
}

If you don't know how many items there will be on each line, use fgets to read the line into a buffer, then use strtok or sscanf on that buffer to read in individual items, e.g.

/* read up to 10 integers from each line of standard input and print the sum for each line */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
char linebuf[1024];  /* A 1024 character buffer is more than enough for this problem */
char* token;
int sum;

  while (fgets(linebuf, sizeof(linebuf), stdin) !=0) {
        sum=0;
        token = strtok (linebuf," \t");  /* we are using space and tab as delimiters */
        while (token != NULL) {
                sum += atoi(token);
                token = strtok (NULL, " \t");
        }
        printf("%d\n",sum);
  }
return 0;
}

When you are testing your program by typing from the terminal, type control-d to signal end of file (EOF).

Read the man pages of sscanf , fgets, strtok, and atoi to understand how these system functions work, what their return values signify, and what libraries you need to include. Remember, you can bring in any printed materials to the competition, so it is a good idea to make and print out some templates for doing input that you can refer to during the competition. The man pages will also be available.


3. How to parse standard input in C++:

There are several ways to do this, including using the C code above. The most common way is to use cin and cout.

// Read in an integer, a string, and a floating point number, and print them out
#include <iostream>
#include <string>
using namespace std;

int main() {
int x;
double y;
string s;
  cin >> x >> y >> s;
  cout << s << endl;
  cout << x << " " << y << endl;
  return 0;
}

You can also use getline. Make sure you know how to use the delimiter argument. Depending on how you write your code, you may have to call getline just to throw away the newline at the end of a line of input.

// reading strings from standard input with getline
#include <iostream>
#include <string>
using namespace std;

int main () {
  string s1,s2,s3;
  getline (cin,s1,' ');
  getline (cin,s2,' ');
  getline (cin,s3);
  cout << s1 << endl;
  cout << s2 << endl;
  cout << s3 << endl;
  return 0;
}

How to read in an undetermined number of lines from standard input:

Basically, you set up a while loop that tests the result returned by the method cin.operator>>, and terminate the loop if the result returned is zero.

while(cin >> x) {
	// process input
}

while(getline(cin, linebuf)) {
	// process input
}

4. How to parse standard input in Java:

Again there are several ways to do this. Here we present only one - using the Scanner class.

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

public class foo
{
  public static void main (String args[])
  {
        Scanner in = new Scanner(System.in);
        String s,linebuf;

        int n = in.nextInt();
        double x = in.nextDouble();
        double y = in.nextDouble();
        char c = in.next().charAt(0);
        s = in.next(); // read in the next token (tokens are separated by whitespace by default)
        in.nextLine(); // throw away the rest of the line
        linebuf = in.nextLine();

        System.out.println(n + " " + x + " " + y + " " + c );
        System.out.print(s+" ");
        System.out.println(linebuf);
  }
}

5. How to parse standard input in Python:

To read a single line of input:

a = raw_input()

To read and process multiple lines of input:

import sys

for line in sys.stdin:
    do_something() 

No guarantee is made regarding the accuracy of information on this page. It is up to you to try it and become familiar with processing standard input yourself.

Personal tools