H: 奇數個的那個數 CSU-1217

Description

給定些數字,這些數中只有一個數出現了奇數次,找出這個數。

Input

每組數據第一行n表示數字個數,1 <= n <= 2 ^ 18 且 n % 2 == 1。

接下來n行每行一個32位有符號整數。

Output

出現奇數次那個數,每組數據對應一行。

Sample Input

5
1
1
2
2
3

7
1
2
1
2
2
3
3

Sample Output

3
2

這題本來的思路是開數組,然後一個個判斷個數,就炸了。

然後由異或的知識,我們可以直接消去偶數個相同的數字,賊帶感。

#include <iostream>
#include <cstdio>
#include <string>5
#include <string.h>
#include <queue>
using namespace std;

int n,ans;

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int x;
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            ans^=x;
        }
        printf("%d\n",ans);
    }
    return 0;
}

/**********************************************************************
	Problem: 1217
	User: jk1601zr
	Language: C++
	Result: AC
	Time:408 ms
	Memory:2024 kb
**********************************************************************/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章