HDU - 2095 find your present (2) 異或運算

          find your present (2)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22595    Accepted Submission(s): 8947


Problem Description
In the new year party, everybody will get a "special present".Now it's your turn to get your special present, a lot of presents now putting on the desk, and only one of them will be yours.Each present has a card number on it, and your present's card number will be the one that different from all the others, and you can assume that only one number appear odd times.For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present will be the one with the card number of 3, because 3 is the number that different from all the others.
 

Input
The input file will consist of several cases. 
Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.
 

Output
For each case, output an integer in a line, which is the card number of your present.
 

Sample Input
5 1 1 3 2 2 3 1 2 1 0
 

Sample Output
3 2
Hint
Hint
use scanf to avoid Time Limit Exceeded


解題思路:

題目內存限制:1024K,所以不能簡單地用數組存然後再處理。

爲了節約內存,可以用STL裏面的set,map等容器。

當容器裏沒有這個元素的時候,就插入這個元素,否則,刪除這個元素。

最後,容器中肯定只剩下一個元素,便是我們所要的結果。

Cpp代碼  收藏代碼
  1. #include <set>  
  2. #include <stdio.h>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     int n,x;  
  7.     set <int> S;  
  8.     while(scanf("%d",&n),n)  
  9.     {  
  10.         while(n--)  
  11.         {  
  12.             scanf("%d",&x);  
  13.             if(S.find(x) == S.end())    //沒找到,插入  
  14.                 S.insert(x);  
  15.             else                        //找到了,刪除  
  16.                 S.erase(x);  
  17.         }  
  18.         printf("%d\n",*S.begin());  
  19.         S.clear();  
  20.     }  
  21.     return 0;  
  22. }  

其實,這題還有個更好的方法————位異或。
 這個題目你要理解2點 第一, 爲什麼要用 異或來算。 第二, 異或是怎麼來算的。  題目給的數據量特別大 如果之前試過用hash算的話,(既開一個1000000的數組來保存每一個數字出現得次數,然後掃描值爲1的) 很可能會Memory Limit Exceeded 。  所以纔想到了用異或。 異或:就是按位比較相同則爲0不同則是1;   比如   3^5,   3=011,5=101,兩數按位異或後爲110,即6。      偶數次出現的異或結果爲0 基數次結果爲1      然後  異或滿足交換率。 再舉一個例子。    如 數據  1 2 3 2 1       先讓result=0     那麼可以看成是  result^1^2^3^2^1         交換律     result^1^1^2^2^3        很明顯      1^1 和  2^2 都爲 0         所以最後得    result^3 =0^3 =3(二進制 101)
整數的異或是先把它們化成二進制,再按位異或。
比如3^5,3=011,5=101,兩數按位異或後爲110,即6。
幾個數異或滿足交換律。2^3^2=2^2^3=0^3=3.
兩個相同的數異或爲0,普通數都出現了偶數次,
所以它們異或後都是0,而0與那個特別數異或後還是那個特殊數。
#include<stdio.h>
int main()
{
    int n,i,x,y;
    while(scanf("%d",&n)!=EOF&&n)
    {
           x=0;
           while(n--)
           {
                scanf("%d",&y);
                x^=y;     
           }         
           printf("%d\n",x);             
    }
    return 0;
}



發佈了35 篇原創文章 · 獲贊 28 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章