ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules

How to write C/C++ solutions

C and C++ programs are compiled on the server with Microsoft Visual C++ 2022 17.8 or GCC 13.2.0 or Clang 17.0.6. All compilers support at least C17 and C++20 standards. cppreference.com is a good starting point for learning modern C and C++.

Visual C++ 2022 has a version included in Visual Studio 2022 v17.8. Compiler is provided targeted for both x86 and x64. You can get the online reference and download the free Visual Studio Community 2022. Compiler is invoked with the following arguments:

// C
cl /TC /MT /EHsc /GL /O2 /W3 /std:clatest /D "ONLINE_JUDGE" %1

// C++
cl /TP /MT /EHsc /GL /O2 /W3 /Za /std:c++latest /D "ONLINE_JUDGE" %1

MinGW GCC 13.2.0 x64 and Clang 17.0.6 x64 are taken from the build provided by WinLibs. The compilers are invoked with the following arguments:

// C
gcc -x c -std=c2x -O2 -fno-strict-aliasing -march=native
    -DONLINE_JUDGE -static -Wl,-s,--stack=67108864 -o %1.exe %1

// C++
g++ -x c++ -std=c++23 -O2 -fno-strict-aliasing -march=native
    -DONLINE_JUDGE -static -Wl,-s,--stack=67108864 -o %1.exe %1

// C++
clang++ -x c++ -std=c++23 -O2 -fno-strict-aliasing -march=native
    -DONLINE_JUDGE -static -pthread -Wl,-s,--stack=67108864 -o %1.exe %1

An example of solving a problem

This is a sample solution for the A + B problem in the C language:

#include <stdio.h>
int main()
{
   int a, b;
   scanf("%d%d", &a, &b);
   printf("%d\n", a + b);
   return 0;
}

And this is a sample solution for the same problem in C++:

#include <iostream>
int main()
{
   int a, b;
   std::cin >> a >> b;
   std::cout << a + b << std::endl;
   return 0;
}

Input/output

An example of how to use input/output is given above. C++ programmers may always choose between two input/output libraries: standard (scanf, printf) or stream (cin, cout) input/output. Stream input/output is easy to use, but it works much slower than standard input/output. This may even cause receiving the Time limit exceeded verdict. Therefore, if you see that a problem requires reading a lot of input data (for example, more than a megabyte) or a large output is expected, then you should better use standard input/output.

Remember that symbol reading functions return negative values for symbols with codes 128-255. If the input data may contain such symbols, then it is better to transform the type of the symbol read to unsigned char explicitly than to get the Wrong answer or Runtime error (access violation) verdicts because of this.

You should know how to read the input data until the end of the stream to solve certain problems. The following examples show how to implement reading input until the end of stream in case input data consists of numbers, lines or separate characters.

#include <stdio.h>
...
// numbers
int n;
while (scanf("%d", &n) != EOF)
{
   ...
}
// lines
char line[1000];
while (gets(line))
{
   ...
}
// characters
int c;
while ((c = getchar()) != EOF)
{
   ...
}
#include <iostream>
...
// numbers
int n;
while (std::cin >> n)
{
   ...
}
// lines
std::string line;
while (std::getline(std::cin, line))
{
   ...
}
// characters
char c;
while (std::cin.get(c))
{
   ...
}

How to use 64-bit integer data types

The compiler fully supports 64-bit integers (both signed and unsigned). A signed 64-bit integer ranges from –9223372036854775808 to 9223372036854775807 and an unsigned 64-bit integer ranges from 0 to 18446744073709551615. A 64-bit variable can be declared in the following way:

long long a;
unsigned long long b;

64-bit variables can be read and written in two ways as well depending on the input/output library used:

#include <stdio.h>
...
scanf("%lld", &a);
scanf("%llu", &b);
printf("%lld", a);
printf("%llu", b);

#include <iostream>
...
std::cin >> a;
std::cin >> b;
std::cout << a;
std::cout << b;

Other notes

You must not throw exceptions in solutions of problems (even inside a block try … catch)—this will be interpreted by the checker as a Runtime error.

Visual C++ Only. In order to increase the size of a stack and to avoid its overflow when using a “deep” recursion, you should use a special directive (in the example, the size of the stack is set to be 16 MB):

#pragma comment(linker, "/STACK:16777216")

It is convenient to use the conditional directive ONLINE_JUDGE for debugging solutions.

#include <stdio.h>
int main()
{
#ifndef ONLINE_JUDGE
   freopen("input.txt", "rt", stdin);
   freopen("output.txt", "wt", stdout);
#endif
   int a, b;
   scanf("%d%d", &a, &b);
   printf("%d\n", a + b);
   return 0;
}

Note that the function freopen may also be used with stream input/output.

#include <iostream>
int main()
{
#ifndef ONLINE_JUDGE
   freopen("input.txt", "rt", stdin);
   freopen("output.txt", "wt", stdout);
#endif
   int a, b;
   std::cin >> a >> b;
   std::cout << a + b << std::endl;
   return 0;
}

Earlier compilers

  • The Intel C++ Compiler 7.0 was used until August 3, 2010.
  • MinGW GCC 4.7.2 was used until October 3, 2014.
  • Microsoft Visual C++ 2010 was used until Septemper 22, 2015.
  • Microsoft Visual C++ 2013 was used until September 1, 2017.
  • MinGW GCC 4.9.1 was used until September 1, 2017.
  • Clang 3.5 was used until September 1, 2017.
  • Microsoft Visual C++ 2017 was used until September 1, 2020.
  • MinGW GCC 7.1 was used until September 1, 2020.
  • Clang 4.0.1 was used until September 1, 2020.
  • Microsoft Visual C++ 2019 was used until January, 22 2024.
  • MinGW GCC 9.2 was used until January, 22 2024.
  • Clang 10.0.0 was used until January, 22 2024.