I'm doing a code-challenge problem:

Captain Kaban: National Treasure is a popular football simulation game which can be played on most smartphones. In this game, you put up a team of 11 players and then compete in either story mode or multi-player mode. As you might already know, a football match is played by 11 players, which consists of one goalkeeper and ten outfield players.

Each outfield player's capability is measured with scores in 3 areas: Attack (dribble, shoot, pass), Defense (tackle, block, intercept), and Physical (speed, power, technique). For example, a player with A:4103 D:2837 P:3410 is a typical attacker player (forward position) as his Attack (4103) is higher than his Defense (2837) and Physical (3410). On the other hand, a player with A:1546, D:5209, P:2708 is a typical defensive player as his Defense is much higher than his Attack and Physical. A goalkeeper has a different measurement (Saving and Physical), but it's not our concern in this problem.

A team strength is simply the sum of all the main outfield player's capability-scores. Note that the team strength score does not consider the team's balance, e.g., a team of 10 players where each player has A:8000, D:1000, P:1000 is stronger than a team where each player has A:3000, D:3000, P:3000; the first team's team strength is 100,000 (10 * (8000 + 1000 + 1000)) while the second one is 90,000 (10 * (3000 + 3000 + 3000)).

Let say you are given a team with ten main outfield players and N reserve players. If you are allowed to substitute at most 1 main outfield player (with a reserve player), what is the maximum team strength you can obtain?


Input begins with an integer: T (1 ≤ T ≤ 20) denoting the number of cases.

Each case contains the following input block: Each case begins with an integer: N (1 ≤ N ≤ 10) denoting the number of reserve players. The next 10 lines each contains three integers: Ai Di Pi (100 ≤ Ai, Di, Pi ≤ 20,000) representing the Attack, Defense, and Physical score for the ith main player. The next N lines each contains three integers: Aj Dj Pj (100 ≤ Aj, Dj, Pj ≤ 20,000) representing the Attack, Defense, and Physical score for the jth reserve player.


For each case, output in a line "Case #X: Y" where X is the case number (starts from 1) and Y is the output for the respective case.

#include <iostream>

using namespace std;

int main () {
    const int totalOriginalPlayer {10};
    int testCases {};
    cin >> testCases;

    for (int i = 1; i <= testCases; i++) {
        int reserve {}, total {}, lowest {500000}, highest {};
        int attack {}, defense {}, physical {};
        cin >> reserve;

        int reservePlayers [reserve];
        int powerReservePlayers [reserve];

        int powerOriginalPlayers [totalOriginalPlayer];

        // Get power of all original players, calculate sum and find the minimum
        for (int i = 0; i < totalOriginalPlayer; i++) {
            cin >> attack >> defense >> physical;
            powerOriginalPlayers[i] = attack + defense + physical;
            total += powerOriginalPlayers[i];

            if (powerOriginalPlayers[i] < lowest) {
                lowest = powerOriginalPlayers[i];

       // Get power of all reserve players and find the maximum power
       for (int j = 0; j < reserve; j++) {
            cin >> attack >> defense >> physical;
            powerReservePlayers[j] = attack + defense + physical;

            if (powerReservePlayers[j] > highest) {
                highest = powerReservePlayers[j];

       // Subtract total with lowest and add highest
       if (lowest < highest) {
            total = total - lowest + highest;
       } else if (lowest >= highest) {
            total = total;

       // Print out total
       cout << "Case #" << i << ": "<< total << endl;

1 Answer 1

  • Don't do using namespace std;.

  • Use an appropriate type for the range. The number of test-cases can never be negative, so we should use an unsigned integer. It looks like the player capabilities are similarly unsigned.

  • for (int i = 1; i <= testCases; i++) In C++, it's more usual to use != for the end condition, and use the pre-increment operator (since we don't a temporary value). It's usually safer to use a zero-based index, and simply add one for printing if we need to convert to one-based: for (int i = 0; i != testCases; ++i).

  • int attack {}, defense {}, physical {};. Declare variables (especially POD variables) as close to the point of use as possible to minimize their scope and avoid reusing them for multiple things. These should be declared in each of the innermost loops.

  • reserve is not a constant value, so this is not valid C++: int reservePlayers [reserve];, even if some compilers allow it. We should use a std::vector instead. (This particular array also appears to be unused!)

  • for (int i = 0; i < totalOriginalPlayer; i++) The loop condition variable shadows the i variable of the outer loop. This is likely to cause confusion. It would be better to pick a meaningful name for the loop variables, e.g. outfielderIndex.

  • It might be neater to separate the logic of the program from reading the input. Then we could reuse the input reading code for both sets of players.

    std::vector<std::uint32_t> outfielders = ReadPlayerData(numOutfielders);
    std::vector<std::uint32_t> reserves = ReadPlayerData(numReserves);
    std::uint32_t total = // ...
    std::uint32_t worstOutfielder = // ...
    std::uint32_t bestReserve = // ...
    // ... do output
  • We can use the standard library to do the necessary math. Specifically std::accumulate, std::min_element and std::max_element.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.