您的位置:首页 > 职场人生

3295 落单的数 九章算法面试题

2017-07-13 08:13 323 查看
时间限制: 1 s

空间限制: 1000 KB

题目等级 : 黄金 Gold

题解
查看运行结果

题目描述 Description

有n个数(n是奇数),其中n-1个数两两成对,有1个数落单,找出这个数。要求O(n)的时间复杂度,O(1)的空间复杂度

输入描述 Input Description

第一行输入一个n, n是大于等于1的奇数

第二行包含n个整数

输出描述 Output Description

输出那个落单的数

样例输入 Sample Input

3

1 7 1

样例输出 Sample Output

7

数据范围及提示 Data Size & Hint

1<=n<=4000001 n是一个奇数

分类标签 Tags 点此展开

对于任意x,

有如下性质

x^y^y=x;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define lli long long int
using namespace std;
int main()
{
int n;
cin>>n;
n--;
int now,x;
cin>>now;
while(n--)
{
scanf("%d",&x);
now^=x;
}
cout<<now;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: