1 solutions

  • 0
    @ 2025-5-14 21:42:34

    直接枚举,当 n=1n=1 时:只有 11 种选择(选自己),直接返回 11。 当 n=2n=2 时: 选 11 块:两个元素各自的值。 选 22 块:两元素之和。 需将所有可能的和存入集合去重。 当 n=3n=3 时: 选 11 块:三个元素各自的值。 选 22 块:三对元素的和(如 a+ba+b, a+ca+c, b+cb+c)。 选 33 块:三元素之和。 同样存入集合去重。

    #include <iostream>
    #include <set>
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
        int a, b, c; // 最多3个变量存储积木高度
        
        // 读取输入
        if (n >= 1) cin >> a;
        if (n >= 2) cin >> b;
        if (n == 3) cin >> c;
        
        set<int> sums; // 用于存储不同的和
        
        // 根据n的不同处理不同情况
        if (n == 1) {
            sums.insert(a);
        } else if (n == 2) {
            sums.insert(a);
            sums.insert(b);
            sums.insert(a + b);
        } else { // n == 3
            sums.insert(a);
            sums.insert(b);
            sums.insert(c);
            sums.insert(a + b);
            sums.insert(a + c);
            sums.insert(b + c);
            sums.insert(a + b + c);
        }
        
        cout << sums.size() << endl;
        return 0;
    }
    
    • 1

    Information

    ID
    1477
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    7
    Tags
    (None)
    # Submissions
    233
    Accepted
    51
    Uploaded By