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<iostream>
#include<cstdio>
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值