1041 Be Unique (20分)

2020-02-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;
}
上一篇下一篇

猜你喜欢

热点阅读