利用set进行数组去重

题目

关于map和set的区别


该题的题解

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <unordered_set>
using namespace std;
typedef long long LL;
LL sum = 0;
unordered_set<int> st;
int main()
{
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
if (temp >= 1 && temp <= k)
{
st.insert(temp);
}
}
sum = (LL)k * (k + 1) / 2; //这里必须要转换为LL类型,否则会溢出。因为k定义的是int类型。
LL sum2 = 0;
for (int t : st)
{
sum2 += t;
}
cout << sum - sum2;
return 0;
}
posted @   electricity111  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示