美文网首页
1041 Be Unique (20分)

1041 Be Unique (20分)

作者: 量化啦啦啦 | 来源:发表于2020-02-17 18:17 被阅读0次
    image.png
    /*
    Sample Input 1:
    7 5 31 5 88 67 88 17
    Sample Output 1:
    31
    Sample Input 2:
    5 888 666 666 888 888
    Sample Output 2:
    None
     */
    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int maxn = 10010;
    vector<int> V(maxn), judge; //maxn个元素均为0
    int main() {
        int N, ans;
        bool unique = false;
        cin >> N;
        for (int i = 0; i < N; i++) {
            int index;
            cin >> index;
            judge.push_back(index);
            V[index] += 1;
        }
        for (int i = 0; i < N; i++) {
            if (V[judge[i]] == 1) {
                ans = judge[i];
                unique = true;
                break;
            }
        }
        if (!unique)   //unique为假,不存在unique的数
            printf("None");
        else
            printf("%d", ans);
        return 0;
    }
    //另解
    #include <cstdio>
    using namespace std;
    int a[100001], m[100000];
    int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            m[a[i]]++;
        }
        for (int i = 0; i < n; i++) {
            if (m[a[i]] == 1) {
                printf("%d", a[i]);
                return 0;
            }
        }
        printf("None");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:1041 Be Unique (20分)

          本文链接:https://www.haomeiwen.com/subject/lflifhtx.html