C++ Practice Problem – Delegation Task

Time Limit: 2s Memory Limit: 192MB

Problem Description

A reconnaissance team has received an urgent task, requiring them to select as many members as possible from six team members: A, B, C, D, E, and F, with the following constraints:

1) At least one of A or B must go;

2) A and D cannot go together;

3) Two of A, E, and F must go;

4) B and C must either both go or both not go;

5) One of C or D must go;

6) If D does not go, then E must also not go.

Who should be sent?

Input Format

None

Output Format

Members to be sent

If there are multiple, list them in alphabetical order, separated by commas (including the trailing comma)

Sample Input

None

Sample Output

A,B,C,F,

Code

#include <iostream>#include <vector>#include <algorithm>using namespace std;
int main() {    vector<string> solutions;    // Enumerate all 64 possibilities (6 people, each can go or not)    for (int i = 0; i < 64; i++) {        bool A = i & 1;        bool B = i & 2;        bool C = i & 4;        bool D = i & 8;        bool E = i & 16;        bool F = i & 32;
        // Condition 1: At least one of A or B must go        if (!(A || B)) continue;
        // Condition 2: A and D cannot go together        if (A && D) continue;
        // Condition 3: Two of A, E, F must go        int count_AEF = A + E + F;        if (count_AEF != 2) continue;
        // Condition 4: B and C must either both go or both not go        if (B != C) continue;
        // Condition 5: One of C or D must go        if (C == D) continue;
        // Condition 6: If D does not go, then E must also not go        if (!D && E) continue;
        // All conditions satisfied, collect results        string s;        if (A) s += "A,";        if (B) s += "B,";        if (C) s += "C,";        if (D) s += "D,";        if (E) s += "E,";        if (F) s += "F,";        solutions.push_back(s);    }
    // Sort in alphabetical order (actually already sorted due to enumeration order)    sort(solutions.begin(), solutions.end());
    // Output all solutions    for (const string& s : solutions) {        cout << s;    }
    return 0;}

Output Result

C++ Practice Problem - Delegation TaskMethodology(1) Enumerate all possibilities: Since there are only 6 people, each with two states (go or not go), there are a total of 2^6=64 possibilities to enumerate.(2) Check conditions: For each possible situation, check if all 6 conditions are met.(3) Collect results: Gather all valid situations and sort them in alphabetical order for output.Code Explanation(1) Enumerate all situations: Use a 6-bit binary number to represent the states of the 6 people (0 means not going, 1 means going).(2) Check conditions: Sequentially check the 6 conditions; if any are not met, skip the current situation.(3) Collect results: Combine the valid members into a string in alphabetical order and store it in the solutions list.(4) Output results: Print all valid combinations.Validation ExampleOutput: A,B,C,F,C++ Practice Problem - Delegation Task

C++ Basic Tutorial Collection

C++ Practice Problem - Delegation TaskC++ Basic Materials

1. C++ Output

2. C++ Variables

3. C++ Input

4. C++ Expressions

5. IF Statements

6. IF Applications

7. WHILE Loop Statements

8. FOR Loop Statements

9. Arrays

10. One-Dimensional Arrays

11. Two-Dimensional Arrays

12. C++ Functions

13. C++ File Operations – Writing Files

If you find it interesting, please click on me

C++ Practice Problem - Delegation TaskC++ Practice Problem - Delegation TaskC++ Practice Problem - Delegation TaskC++ Practice Problem - Delegation Task

Leave a Comment