计蒜客————单独的数字(二)

来源:互联网 发布:java单引号和双引号 编辑:程序博客网 时间:2024/06/05 20:12

一个整型数组中有一个元素只出现一次,其它元素都出现两次。求出只出现一次的元素。

要求:

线性时间复杂度,不能使用额外空间。

聪明的你能搞定吗?


格式:

第一行输入数字n,代表有n个数,根据题意,很明显n是奇数,

第二行输入数组A[i], i从0~n-1.

最后输出单独的数字。


样例输入

71 3 2 0 3 2 1这道题的归类是位运算,利用两个相同的值异或为0,来求得唯一不同的值。
#include <iostream>#include<cstring>#include<stdio.h>#include<algorithm>#include<map>#define MAX 9223372036854775807using namespace std;int main(){    int n;    while(~scanf("%d",&n))    {        int com;        scanf("%d",&com);        n--;        while(n--)        {            int t;            cin>>t;            com=com^t;        }        printf("%d\n",com);    }    return 0;}