Interactive tasks

eesti keeles | по-русски

In interactive tasks, the solution communicates with the testing environment using standard input and output instead of files. This is quite similar to using files, the main difference being that your programming environment already has special objects that correspond to standard streams. These streams are always open and your program can start reading the input and writing the output immediately.

C

Reading from standard input: like regular reading but use stdin as the file variable. For example, to read an integer:

    fscanf(stdin, "%d", &n);

Writing to standard output: like regular writing but use stdout as the file variable. In interactive tasks, you also need to take care of flushing the output buffer, using the fflush function:

    fprintf(stdout, "%d\n", n);
    fflush(stdout);

The stdin and stdout variables and the fflush function are all declared in header file stdio.h.

C++

Reading from standard input: like regular reading but use cin as the file variable. For example, to read an integer:

    std::cin >> n;

Writing to standard output: like regular writing but use cout as the file variable. In interactive tasks, you also need to take care of flushing the output buffer; the easiest way to do it is to use endl as the end-of-line marker instead of "\n":

    std::cout << n << std::endl;

The cin, cout and endl objects are all declared in header file iostream.

C#

Reading from standard input: like regular reading but use Console.In as the file variable. For example to read a line:

    string s = Console.In.ReadLine();

Writing to standard output: like regular writing but use Console.Out as the file variable. In interactive tasks, you also need to take care of flushing the output buffer. The best way for this is the Flush method:

    Console.Out.WriteLine(s);
    Console.Out.Flush();

The Console object is declared in the System namespace.

Java

Reading from standard input: like regular reading but use System.in as the file variable. To read line by line, use BufferedReader:

    BufferedReader in = new BufferedReader(System.in);
    String s = in.readLine();

Writing to standard output: like regular writing but use System.out as the file variable. In interactive tasks, you also need to take care of flushing the output buffer. The best way for this is the flush method:

    System.out.println(s);
    System.out.flush();

The System class is declared in the java namespace, and the BufferedReader class in java.io namespace.

Pascal

Reading from standard input: like regular reading but use input as the file variable. For example, to read an integer:

    read(input, n);

Writing to standard output: like regular writing but use output as the file variable. In interactive tasks, you also need to take care of flushing the output buffer, using the flush function:

    writeln(output, n);
    flush(output);

Python

Reading from standard input: like regular reading but use sys.stdin as the file variable. For example, to read a line:

    s = sys.stdin.readline()

Writing to standard output: like regular writing but use sys.stdout as the file variable. In interactive tasks, you also need to take care of flushing the output buffer, using the flush function:

    sys.stdout.write(s + "\n")
    sys.stdout.flush()

The sys namespace must be imported to the program before use.

Lehekülg viimati muudetud October 13, 2016, at 02:54 PM