SDNU1541 Your Code Is Awesome(二进制异或/12.16期末赛)

你代码写的真棒.jpg

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

Solution

关于位运算:https://blog.csdn.net/m0_37602175/article/details/72831779
大概过程是 全部异或(得到A^B)–>得到A ^ B中为1的部分,求出最后一个为1的数字的位置–>遍历,异或所有在该位置为1的数A(异或即可求出另一个数B,不需要考虑其他满足的数)–>再次异或求解
注意数组开在外面,在里面会RE- -

AC代码

我背的

#include
#include
using namespace std;
int c[1000001];
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;++i)
    {
        int n;
        int a,b;
        scanf("%d",&n);
        int s=0;
        for(int j=1;j<=n;++j)
        {
            scanf("%d",&c[j]);
            s=s^c[j];	//全部异或
        }
        a=s;
        int m=0;
        while(!(s>>m&1)) ++m;	//找0之前的1
        for(int j=1;j<=n;++j)
        {
            if(c[j]>>m&1) a=a^c[j]; //异或求b
        }
        b=s^a;	//再次异或
        if(a>b) printf("%d %d\n",b,a);	//比较大小输出
        else printf("%d %d\n",a,b);
    }
    return 0;
}

你可能感兴趣的:(sdnu)