sdnuoj1541(异或)

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(), denoting the number of testcases.For each testcase, there is an integer , then following integers , 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

用map都会TLE。。应该用异或,先求异或和,判断出其二进制最右边的1的位置,然后遍历一遍,把该位为1和该位为0的数分开分别异或(相同的数一定会被分在同一组~),最后得到的这两组分别的异或和就是单出来的那俩数。(时隔5个月,我终于懂了hhhh)
注意(f&sum)需要加括号

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int a[N];
int main()
{
    int t,n,m,i,f;
    long long sum;
    scanf("%d",&t);
    while(t--)
    {
        sum=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum^=a[i];
        }
        f=1;
        while((f&sum)==0)
            f<<=1;
        int x,y;
        x=y=0;
        for(i=0;i<n;i++)
        {
            if((f&a[i])!=0)
                x^=a[i];
            else
                y^=a[i];
        }
        if(x>y)
        {
            int r=x;
            x=y;
            y=r;
        }
        printf("%d %d\n",x,y);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值