【2012年腾讯俱乐部ACM赛生手组1004】XOR的用法-Poor man on the Single’s Day

【2012年腾讯俱乐部ACM赛新手组1004】XOR的用法-Poor man on the Single’s Day

这个是真没想到,CP的DISCUSS,给自己提个醒,原来位运算这么管用。有时会有奇效~

Description

   The Galapagos Islands are a nature lover's paradise. Isolated from the mainland for thousands of years, some of the most unusual species in the world evolved here, including giant tortoises, marine iguanas, blue-footed boobies, and even the world's only equatorial penguin. The islands comprise the Galapagos National Park and were recently named a "World Heritage Site".

  On such a mysterious and beautiful island, there is a happy tribe. All the guys in this tribe are in pairs except one poor man. On the Single’s Day, can you help me find out this poor man?

  Every guy in the tribe has an id, if two guys are in pairs, they will have the same id. For example, there are five guys in the tribe, their ids are: 3, 9, 9, 3, 7. It means that the man with the id 7 is the poor man that we want to find out.

Input

      Input consists of multiple test cases.   

      The first line contains a number: n(there are n guys in the tribe); 0 < n <= 10 000 000 and n is an odd number;
And then there are n ids. The ids are out-of-order and in pairs except one id. 0 < id <= 10 000 000

Output

      The id of the poor single man.

Sample Input
【2012年腾讯俱乐部ACM赛生手组1004】XOR的用法-Poor man on the Single’s Day Copy sample input to clipboard
7
3 9 9 3 3 3 10
Sample Output
10

#include <iostream>  
#include <cstring>  
#include <cstdlib>  
#include <cstdio> 
#include <cmath>  
 
int n,t,ans=0; 
 
int main() 
{  
    while  (scanf("%d",&n)!=EOF)  
    {  
        for (int i=1;i<=n;i++) 
        {  
            scanf("%d",&t); 
            ans^=t; 
        }  
        printf("%d\ n",ans);  
        ans=0;  
    }
    return 0;
}