Back to guide home
Language Basics

C++ / Python shapes used in this practice site

You do not need a full grammar course first. Input, output, loops, conditions, and arrays are enough to begin most starter problems.

Read one integer

int n;
cin >> n;
n = int(input())

Read a line of numbers

vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
a = list(map(int, input().split()))

Print the answer

cout << answer << '\n';
print(answer)

Condition

if (x >= k) {
    answer++;
}
if x >= k:
    answer += 1

Loop through an array

for (int x : a) {
    total += x;
}
for x in a:
    total += x

Sort

sort(a.begin(), a.end());
a.sort()

Remember these three ideas

  • Variable names should match story objects.
  • Loops usually process input records one by one.
  • An if statement translates a rule from the problem.
Next: how the practice works