SDNUOJ——1541.Your Code Is Awesome
Description
There is an ACMer named The_Flash, who can write g♂♂d code in SDNU ACM Traing Team. With his excellent coding skills, he has won a lot of praises.
Now, he gives you an easy problem to solve, the problem is showen as follows.
Give you a sequence with
integers, it is guaranteed that only two different integers appear once and other integers are all appear twice. You are expected to find out that two “single” integers.
Input
The first line is an integer T(T<10), denoting the number of testcases.
For each testcase, there is an integer 2<=n<=1000000, then following integers 0<ai<=1000000000, there is a space between every two integers.
Output
For each testcase, output two integer, denoting the answer. (In order from small to large).
Sample Input
2
2
1 2
10
1 1 2 2 3 3 4 4 6 5
Sample Output
1 2
5 6
题意:给出一堆数组,其中只有两个数字没有成对出现,求这两个数字。
分析:异或
#include<stdio.h>
#include<iostream>
#include<map>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<string>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
#define MAXN 1000005+20
#define INF 0x3f3f3f3f//将近int类型最大数的一半,而且乘2不会爆int
const double T = 3.1415927;
int a[MAXN];
int main()
{
int t, x1, x2, sum, ans, n;
scanf("%d", &t);
while(t--)
{
n, sum=0;
scanf("%d", &n);
for(int i=0; i<n; ++i)
{
scanf("%d", &a[i]);
sum^=a[i];
}
ans=1;
while((sum&ans) == 0) 找出最右边第1个不为0的位置
ans<<=1;
x1=0, x2=0;
for(int i=0; i<n; ++i) 根据第一个不为0的位置重新将数组进行划分
if(a[i]&ans)
x1^=a[i];
x2 = sum^x1;
cout << min(x1, x2) << ' ' << max(x1, x2) << '\n';
}
return 0;
}