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

时间:2022-05-10
本文章向大家介绍3295 落单的数 九章算法面试题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

时间限制: 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;

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #define lli long long int 
 6 using namespace std;
 7 int main()
 8 {    
 9     int n;
10     cin>>n;
11     n--;
12     int now,x;
13     cin>>now;
14     while(n--)
15     {
16         scanf("%d",&x);
17         now^=x;
18     }
19     cout<<now;
20     return 0;
21 }