Skip to content

Your first algo program

This tutorial assumes you already cloned the repo and can run the commands from the quickstart.

Start with the smallest useful program: read two integers and print their sum.

input:
a, b: int
println a + b

Compile and run it:

Terminal window
./algo compile aplusb.alg -o aplusb.cpp
g++ -std=c++17 -O2 aplusb.cpp -o aplusb
echo "3 4" | ./aplusb

Output:

7

This shows the core workflow:

  1. write an .alg file
  2. compile it to .cpp
  3. compile the C++
  4. run the binary

The generated C++ is ordinary contest-style code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int a, b; cin >> a >> b;
cout << (a + b);
cout << "\n";
return 0;
}

The compiler is not hiding a VM or custom runtime here. You can inspect the output, debug it, and reason about what it will do at submission time.

  • input: reads values in declaration order
  • a, b: int declares two integers
  • println prints one line

That is enough for a large class of introductory contest problems.

Codeforces 4A (Watermelon): given weight w, can it be split into two even positive parts?

input:
w: int
if w > 2 and w % 2 == 0:
println "YES"
else:
println "NO"
Terminal window
./algo compile watermelon.alg -o watermelon.cpp
g++ -std=c++17 -O2 watermelon.cpp -o watermelon
echo "8" | ./watermelon
echo "5" | ./watermelon

Outputs:

YES
NO

This adds two new pieces of syntax:

  • if / else uses indentation instead of braces
  • string literals print directly with println

Solving a real problem →