Lesson 6 of 12

Read the text the problem actually gives

Choose words or full lines, preserve empty cases, and reset only the state that needs it.

A token and a line are different

std::cin >> s reads the next whitespace-delimited token. It might be a word, a code, or punctuation. std::getline(std::cin, s) reads the whole remaining line, preserving its spaces and discarding the newline.

For HNU ICPC Community, two token reads produce HNU and ICPC, leaving Community unread. One getline receives the entire text. An empty line is a valid string of length zero.

A leftover newline is still input

Reading a number with >> leaves its line ending. An immediate getline may therefore read an empty remainder of that same line. When the format puts the desired text on the next line, ignore through the newline once after the number.

Do not apply this fix blindly. If the text belongs on the number’s line, discarding that line loses useful input. If you only read tokens, no ignore is needed. getline already consumes its delimiter, so repeated line reads do not need repeated ignore calls.

C++23
#include <iostream>
#include <string>
#include <limits>

int main() {
    int t = 0;
    std::cin >> t; // Count on its own line; 1 <= t <= 100.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    for (int test = 0; test < t; ++test) {
        std::string line;
        std::getline(std::cin, line);
        std::cout << line.size() << '\n';
    }
    return 0;
}

Reset state, preserve the input boundaries

For multiple cases, declare each sum, count, or candidate inside the case loop. Reusing an array is fine when the current n cells are overwritten and every scan uses the current n. Old values in inactive cells then do not matter.

This does not mean arrays never need clearing: an array used as accumulated state may need a reset. Understand which values will actually be read next. For line cases, an empty line still counts as a case and must not be silently skipped.

Pause & predict

Input is an integer on its own line followed by three lines that may be empty. Where should ignore be called?

Choose an answer
Try it yourself

Turn the idea into code

Read t cases. Each case gives 1 ≤ n ≤ 1000 integers. Print the maximum of each case. Test one case with maximum 90 followed by the single value −5.

Give me a hint

Initialize the candidate from the first element of the current case, not from an earlier case.

Show the explained solution

The two suggested answers are 90 and −5. Each case’s fresh candidate and active length keep state independent.

C++23
#include <iostream>

int main() {
    int a[1000]{};
    int t = 0;
    std::cin >> t;
    for (int test = 0; test < t; ++test) {
        int n = 0;
        std::cin >> n;
        for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
        int best = a[0];
        for (int i = 1; i < n; ++i) {
            if (a[i] > best) { best = a[i]; }
        }
        std::cout << best << '\n';
    }
    return 0;
}

Your notes

Ready for the next idea?

Mark complete when you can explain it yourself.

From a problem to a programIdentify the input, output, and constraints, then translate a small plan into C++.Variables, types, and remembered valuesChoose a representation, initialize it, and understand what assignment really changes.Arithmetic without surprisesPredict division and remainder, widen before multiplication, and format decimal answers.Conditions and logical decisionsBuild exclusive categories, combine comparisons, and protect operations with short-circuit logic.Optional: other ways to chooseRecognize switch for exact choices and the conditional operator for choosing a value.For loops: make repetition preciseTrace initialization, condition, body, and update—and avoid an extra iteration.While, do-while, and stopping rulesChoose a loop from its stopping condition and read the exact input format.Break, continue, and early answersDistinguish leaving a loop from skipping one iteration, without losing required input.Five patterns worth understandingUse sums, counts, best-so-far candidates, products, and flags to process a stream.Nested loops: rows and columnsTranslate a shape into two loop roles and count the total work.Take a number apart, one digit at a timeUse remainder and division to process decimal digits, including zero.Multiple cases and a debugging mindsetCombine the patterns, isolate each case, and test the boundaries that can break your idea.Arrays: give every value a placeUnderstand indices, initialization, capacity, active length, and the first safe array loop.Scan, count, search, and compareDistinguish values from positions and preserve the exact tie rule a problem asks for.Change a sequence without losing itUpdate, copy, compare, reverse, and inspect neighbours using the correct bounds.Matrices: think in two coordinatesTraverse tables, total rows and columns, inspect diagonals, and protect grid neighbours.Characters become stringsSeparate characters from numbers, then safely inspect and edit managed text.Read the text the problem actually givesChoose words or full lines, preserve empty cases, and reset only the state that needs it.Scan text: count, filter, transformBuild a result from matching characters without losing leading zeros or skipping shifted text.Join, compare, and find textLearn content comparison, lexicographical order, substring counts, and the not-found sentinel.