计蒜客 15 map遍历
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
map<int,int>q;
int n,x;
scanf("%d",&n);
while(n--)
{
scanf("%d",&x);
if(q.count(x))
q[x]++;
else
q[x]=1;
}
map<int ,int >::iterator it;
for(it=q.begin();it!=q.end();++it)
{
if(it->second==1)
{
cout<<it->first;
}
}
return 0;
}
这一题因为没有给出n的数据范围,可能会非常大。那么数组开太大就会爆。所以这里用STL的map模板最合适,刚好学习一下map的遍历知识
给定一个数组,除了一个数出现1次之外。其余数都出现3次。
找出出现一次的数。
如:{1, 2, 1, 2, 1, 2, 7}, 找出7.
格式:
第一行输入一个数n。代表数组的长度。接下来一行输入数组A[n],(输入的数组必须满足问题描写叙述的要求),最后输出仅仅出现一次的数。
要求:
你的算法仅仅能是线性时间的复杂度,而且不能使用额外的空间哦~
例子1
输入:
4
0 0 0 5
输出:
5
浙公网安备 33010602011771号