1 条题解

  • 0
    @ 2026-10-23 18:34:34

    P76015 缺少的颜色 — 题解

    专题: 模拟与枚举
    适用题目: 新生 OJ 本地题包中的 P76015,不要直接用于外站同名题。

    解题思路

    四个颜色中,相同颜色只算一类;答案等于 4 - 不同颜色数。

    复杂度

    时间 O(42)O(4^2),额外空间 O(1)O(1)。

    易错点

    不必购买同种颜色多件,目标是四种不同颜色。

    C11 参考程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <limits.h>
    
    int main(void) {
        int a[4], distinct=0;
        for (int i=0;i<4;i++) scanf("%d",&a[i]);
        for (int i=0;i<4;i++) {int exists=0;
            for(int j=0;j<i;j++) if(a[i]==a[j]) exists=1;
            if (!exists) distinct++;
        }
        printf("%d\n",4-distinct);
        return 0;
    }
    

    C++17 参考程序

    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    #define nl '\n'
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        set<int> s;
        for(int i=0;i<4;i++){
            int x;
            cin>>x;
            s.insert(x);
        }
        cout<<4-s.size()<<nl;
        return 0;
    }
    

    题解建议设置为训练结束后开放。普通判题无法验证是否实际使用指定函数或 STL。

    • 1

    信息

    ID
    1784
    时间
    2000ms
    内存
    256MiB
    难度
    1
    标签
    提交数
    0
    已通过
    0
    上传者