题目描述
There is a sequence S consisting of N numbers, where N is an odd number. All numbers in this sequence appear even number times, except one showing up odd number times, which we call unique number. For example, in the sequence {3, 4, 4, 9, 3, 5, 3, 3, 9}, the number 5 is the unique number. Now you are required to work out the unique number for given sequence.
输入
Input file consists of several cases.The first line of each case contains a number N. Each of the next N lines contains only one number Si. We guarantee that the number N is no more than 100,000, and Si is no more than 2^31.
输出
For each case, just return the unique number in one line.
样例输入
9
3
4
4
9
3
5
3
3
9
样例输出
5
提示
Try O(n) algorithm.
来源
#include<stdio.h>
#include<string.h>
int comp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int main()
{ int i,n;
while(scanf("%d",&n)!=EOF)
{
int num=0;
int a[n],m;
for(i=0;i<n;i++)
scanf("%ld",&a[i]);
qsort(a,n,sizeof(a[0]),comp);
for (i=0;i<n;i++){
if (i==0||a[i]!=a[i-1]) {//第一个和每一个新出现的第一个
if (num%2) {
printf("%d\n",a[i-1]);
break;
}
num=1;//每一个新出现数的第一个记为1
}
else num++;
}}
return 0;
}