异或操作

  • 1至1000里面只有一个重复,现在需要找出这个数
  • 一个数组中只有一个数字发生了一次,其他的都是两次,找出这个数字
  • 一个数组中有两个数发生了一次,其他的都是两次,找出这两个数

  • 这里这个题目都是用抑或操作来处理数组里面的数据。

    主要是下面这个公式

    // NOTICE : B = A ^ (B ^ A)

    第一题

    直接上代码了

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    
    const int N = 10;
    
    int main()
    {
        int arr[N];
        int len = sizeof(arr) / sizeof(int);
        int i = 0;
        int res = 0;
        for (i = 0; i < N; i++)
        {
            arr[i] = i + 1;
            if (i == 0)
                res = i + 1;
            else
                res ^= i + 1;
        }
    
        int arr_data[N+1] = {1, 3, 5, 7, 9, 10, 2, 3, 4, 6, 8};
        for (i = 0; i < N+1; i++)
            res ^= arr_data[i];
        std::cout << "only repeat number: " << res << std::endl;
    
        return 0;
    }

    上面将N改为1000即可。


    第二题

    对于第二题,较为容易,对整个数据取异或,剩下的那个值就是发生一次的数。

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    
    const int N = 10;
    
    void quicksort(int arr[], int low, int high)
    {
        if (low >= high)
            return;
        int i = low;
        int j = high;
        int val = arr[j];
        while (i < j)
        {
            while (i < j && arr[i] >= val)
                i++;
            arr[j] = arr[i];
            while (i < j && arr[j] < val)
                j--;
            arr[i] = arr[j];
        }
        arr[i] = val;
        quicksort(arr, low, i-1);
        quicksort(arr, i+1, high);
    }
    
    int main()
    {
        int arr[] = {1, 3, 4, 3, 5, 7, 5, 9, 6, 1, 7, 9, 4};
        int len = sizeof(arr) / sizeof(int);
        int i = 0;
        std::cout << "org data:" << std::endl;
        for (i = 0; i < len; i++)
            std::cout << arr[i] << " ";
        std::cout << std::endl;
    
        /*
        quicksort(arr, 0, len-1);
        std::cout << "sorted data:" << std::endl;
        for (i = 0; i < len; i++)
            std::cout << arr[i] << " ";
        std::cout << std::endl;
        */
    
        int res = 0;
        for (i = 0; i < len; i++)
        {
            if (i == 0)
                res = arr[i];
            else
                res ^= arr[i];
        }
        std::cout << "the only number: " << res << std::endl;
        return 0;
    }


    第三题

    对于第三题,也是一样的,先将这个数组分组,分组以后再来获取相应的值就可以了。

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    
    const int N = 10;
    
    void quicksort(int arr[], int low, int high)
    {
        if (low >= high)
            return;
        int i = low;
        int j = high;
        int val = arr[j];
        while (i < j)
        {
            while (i < j && arr[i] >= val)
                i++;
            arr[j] = arr[i];
            while (i < j && arr[j] < val)
                j--;
            arr[i] = arr[j];
        }
        arr[i] = val;
        quicksort(arr, low, i-1);
        quicksort(arr, i+1, high);
    }
    
    int main()
    {
        int arr[] = {1, 3, 4, 3, 5, 7, 5, 9, 6, 1, 12, 7, 9, 4};
        int len = sizeof(arr) / sizeof(int);
        int i = 0;
        std::cout << "org data:" << std::endl;
        for (i = 0; i < len; i++)
            std::cout << arr[i] << " ";
        std::cout << std::endl;
    
        /*
        quicksort(arr, 0, len-1);
        std::cout << "sorted data:" << std::endl;
        for (i = 0; i < len; i++)
            std::cout << arr[i] << " ";
        std::cout << std::endl;
        */
    
        int res = 0;
        for (i = 0; i < len; i++)
        {
            if (i == 0)
                res = arr[i];
            else
                res ^= arr[i];
        }
        std::cout << "the split number: " << res << std::endl;
        int key = 1;
        while (1)
        {
            if (key & res)
                break;
            else
                key <<= 1;
        }
        std::cout << "the key is " << key << std::endl;
        int number1 = 0;
        int number2 = 0;
        for (i = 0; i < len; i++)
        {
            if (arr[i] & key) {
                if (number1 == 0)
                    number1 = arr[i];
                else 
                    number1 ^= arr[i];
            } else {
                if (number2 == 0)
                    number2 = arr[i];
                else
                    number2 ^= arr[i];
            }
        }
        std::cout << "the number which happens once: " << number1 << ", " << number2 << std::endl;
    
        return 0;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值