God Chang's interview——异或

第一个问题如下

Problem Description
It’s God Chang’s third year of college,he decided
to study in the sougou company,God Chang’s resume is very good,he faced the first interview sooner .At the interview,HR come out a problem:“there are N integer,only one of them exit odd times,please output
that integer,2<=N<=10^6;0
The problem is too easy to defeat God Chang. But because it’s the
first interview he faced in his life.He is a little nervous.So that he
forget everything!!!

As his friends could you help him to solve the problem…

Input
There will multiple test cases; the number of test case T (T ≤
10) is given in the first line of input.

In the first line of every case, there is an integer n (1 ≤ n ≤ 10^6),
the number of integer.

Each of the next n lines including a integer(belong to int)

Output
For each test case ,output one integer in one line

Sample Input
1 3 3 6 6

Sample Output
3

第二个问题如下:

Problem Description
God Chang is a veteran in battle,he passed all the OJ ever. There is a T10 from baidu company,his level is much higher than the HR,God Chang talk cheerfully and humorously with him。

After a short time blank in the brain,God Chang adjust the state quickly and Accepted that water problem。

The HR blushed,He felt his dignity is challenged.So,he raised another question。

“there are N integer,only two of them exit odd times,please output these two integers,2<=N<=10^6;0

HR thought everything is in control. We called it”too young, too simple, sometime naive”. God Chang quickly Accepted that water problem again,Passed the interview smoothly。

Could you solve the problem from HR this time?

Input
There will multiple test cases; the number of test case T (T ≤ 10) is given in the first line of input.

In the first line of every case, there is an integer n (1 ≤ n ≤ 10^6), the number of integer.

Each of the next n lines including a integer(belong to int)

Output
For each test case ,output two ordered integer(separate by a blank) in one line

Sample Input
1
2
3 6
Sample Output
3 6

两道题乍一看都是水题,第一反应就是sort排一下序就好了,没想到一直WA,后来又用了set做,依旧WA。百思不得其解,没想到最后竟然是用异或做的ORZ。这两道题都利用了异或的一个性质:a^a=0;
不过第二题稍稍复杂些,遍历得到的是所求两个数的异或和,怎么将两个数分开呢?想法是异或和中至少有一位是1,而1是由1^0得来的,所以将数组中所有该位为1的再异或,即可得其中一个数。具体做法是先不断取余2来确定异或和的最小为一的位置,比如第三位为1就转化为4,判断(4&num)的值是否为0即可判断num的第三位是否为1。最后输出要从小到大,交换两个数的值也可以用异或来写。

/***********************
       XOR 
    a^a=0;a^0=a;
************************/
#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t,n;
    int i,j,k;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int ans=0,num[n];
        int a,b;
        int x,y;
        for(i=0;i<n;i++){
            scanf("%d",&num[i]);
            ans=ans^num[i];
        }
        x=1;y=ans;a=ans;
        while(y%2==0)
        {
            x=x*2;y=y/2;
        }
        for(i=0;i<n;i++){
            if((x&num[i])!=0) {a=a^num[i];}
        }
         b=ans^a;
        if(a>b){
            a=a^b;
            b=a^b;
            a=a^b;
        }
        printf("%d %d\n",a,b);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值