Repair the Chess Set
National Olympiad — Municipal round (2022 season)
Submission name: chess.cpp
Problem summary
A complete chess set has, for each colour, 1 king, 1 queen, 2 rooks, 2 knights, 2 bishops and 8 pawns. Given the current counts for white and black, report how many of each piece to add; negative counts mean remove pieces.
Input
Two rows of six integers: kings, queens, rooks, knights, bishops, pawns for white, then black.
Output
Two rows of six signed adjustments in the same order.
Constraints
Every supplied piece count is between 0 and 16 inclusive.
Worked example
An independently prepared example for this explanation.
Input
0 1 2 2 2 7 0 1 2 1 2 7
Output
1 0 0 0 0 1 1 0 0 1 0 1
Solution analysis
- Keep the target counts in an array {1,1,2,2,2,8}. Each input count is independent of all others.
- For each colour and piece type, output target−current. Positive differences add missing pieces, negative differences remove extras, and zero leaves a correct count unchanged.
- In the sample, white lacks one king and one pawn. Black lacks a king, a knight and a pawn. The two output rows express those additions in the same piece order as the input.
Why it works
Adding target−current to the current count gives exactly target for every colour and type. The sign also matches whether the current count is below or above the target, so every adjustment has the required interpretation.
Watch out. Subtract current from target, not the reverse. Treat the two colours separately and preserve piece order.
Commented C++17 solution
An independently written complete program with standard headers.
#include <iostream>
using namespace std;
int main() {
int target[6] = {1, 1, 2, 2, 2, 8};
for (int colour = 0; colour < 2; colour++) {
for (int piece = 0; piece < 6; piece++) {
int current;
cin >> current;
if (piece > 0) {
cout << ' ';
}
// Positive means add; negative means remove.
cout << target[piece] - current;
}
cout << '\n';
}
}