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

相关推荐

10-09 00:50
已编辑
长江大学 算法工程师
不期而遇的夏天:1.同学你面试评价不错,概率很大,请耐心等待;2.你的排名比较靠前,不要担心,耐心等待;3.问题不大,正在审批,不要着急签其他公司,等等我们!4.预计9月中下旬,安心过节;5.下周会有结果,请耐心等待下;6.可能国庆节前后,一有结果我马上通知你;7.预计10月中旬,再坚持一下;8.正在走流程,就这两天了;9.同学,结果我也不知道,你如果查到了也告诉我一声;10.同学你出线不明朗,建议签其他公司保底!11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
11-03 14:38
重庆大学 Java
AAA求offer教程:我手都抬起来了又揣裤兜了
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务