题解 | 明明的随机数

明明的随机数

https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0

使用数组去重

#include <bits/stdc++.h>

using namespace std;

const int N = 1e3 + 10;
array<bool, N> a;

int main() {
    int n, m;
    while(scanf("%d", &n) != -1){
        while(n -- ){
            scanf("%d", &m);
            a[m] = true;
        }
    }
    for(int i = 0; i < N; i ++ ){
        if(a[i]) printf("%d\n", i);
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

常规解法使用hash set 去重

#include <bits/stdc++.h>

using namespace std;

vector<int> a;
unordered_set<int> s;

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i ++ ){
        int t;
        scanf("%d", &t);
        if(!s.count(t)){
            a.push_back(t);
        }
        s.insert(t);
    }
    sort(a.begin(), a.end());
    for(const auto &i : a){
        printf("%d\n", i);
    }
    return 0;
}

全部评论

相关推荐

08-15 01:16
Python
Java小萌新新萌小...:照片不用整这么大的 而且你的照片截歪了 你想找专业对口的 那普通话证写在这里其实没有什么必要 就是看着内容多点 而且里面字体大小也不一样 修改一下排版 有很多空间可以再利用一下 字大一点 不然现在这样观感不太好 再就是项目好好优化一下 加油
点赞 评论 收藏
分享
想玩飞盘的菠萝蜜在春...:上交✌🏻也拒?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务