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;
}
全部评论

相关推荐

Java面试先知:我也是和你一样的情况,hr 说等开奖就行了
点赞 评论 收藏
分享
后端转测开第一人:再怎么劝退也没用的 某些群体总以为在一个幸存者偏差的软件上看见了极少数秋招上岸某个大厂的个例就幻想上了 事实上自己打开ssob沟通1000+连个小厂面试都没
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务