题目:http://pat.zju.edu.cn/contests/pat-a-practise/1041
题解:
记录数字出现次序和一个数字出现多少次,然后按出现次数从小到大排序,相同次数按出现次序从小到大排序,最后判断第一个数的出现次数是否为1.
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<algorithm>
using namespace std;
struct number
{
int idx;
int into;
int countx;
}num[10005];
map<int,int> mapx;
bool cmp(const struct number &a,const struct number &b)
{
if(a.countx<b.countx) return true;
else if(a.countx==b.countx) return a.into<b.into;
else return false;
}
int main()
{
int n,a,idx=0;
scanf("%d",&n);
for(int i=0;i<n;++i)
{
scanf("%d",&a);
if(mapx.find(a)==mapx.end())
{
num[idx].countx=1;
num[idx].idx=a;
num[idx].into=i;
mapx.insert(make_pair(a,idx));
++idx;
}
else
{
num[mapx.find(a)->second].countx++;
}
}
sort(num,num+idx,cmp);
if(num[0].countx==1)
printf("%d\n",num[0].idx);
else
printf("None\n");
return 0;
}
来源: http://blog.csdn.net/acm_ted/article/details/20135099