Find Different

Find Different

 

Time Limit:   2000MS       Memory Limit:   65535KB
Submissions:   59       Accepted:   14

 

Description
Give you n*2+1 numbers,in these numbers ,it have n pairs are the same.Please find out which number has no pair.

 

Input
It has only one test case The first line input the number n(1 <= n <= 1000000). The second line input n*2+1 numbers,these numbers should be less than 10^10;

 

Output
Output the number which has no pair.

 

Sample Input

 

1
2 1 2

 

Sample Output

 

1

 

Hint
The first line input 1,it has 1*2+1 numbers.
In the input 3 numbers 1 has no pair,so you should output 1;

In your program you'd better use scanf("%d",&a[i]),or you will get Time Limit Exceeded.

 

解析:

这道题难点在于数的存储,10^10,数组的长度是不可能存到这么长的;这里就以题中的1000000为界,如果num<=1000000,sign[num]++;否则让sign[k]=num,这种方法在存数的很多时候都很实用,具体见代码:

 

# include<stdio.h>
# include<string.h>
int sign[2000005];
int Judge(int num,int k)
{
    int i,leap=0;
    for(i=1000001;i<k;i++)
    {
        if(sign[i]==num)//如果这个大数(num>1000000)出现过,就匹配掉
        {
            sign[i]=0;
            leap=1;
            break;
        }
    }
    return leap;
}
int main()
{
    int n,num;
    int k=1000001;
    int len,i,leap=0;
    memset(sign,0,sizeof(sign));
    scanf("%d",&n);
    len=2*n+1;
    for(i=0;i<len;i++)
    {
        scanf("%d",&num);
        if(num<=1000000)
        sign[num]++;
        else if(!Judge(num,k))//如果这个大数没出现过,就挨着存下
            sign[k++]=num;
    }
    for(i=0;i<=1000000;i++)//在1000000前,如果sign[i]是奇数,那这个数就一定no pair
    {
        if(sign[i]%2)
        {
            printf("%d\n",i);
            leap=1;
            break;
        }
    }
    if(!leap)
    {
        for(i=1000001;i<k;i++)//在大于1000000时,如果sign[i]非零,那就一定是它
        {
            if(sign[i])
            {
                printf("%d\n",sign[i]);
                break;
            }
        }
    }
    return 0;
}

 

 

 

posted on 2013-02-25 22:02  即为将军  阅读(297)  评论(0)    收藏  举报

导航