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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值