异或操作
来源:互联网 发布:请假流程数据库设计 编辑:程序博客网 时间:2024/05/17 05:59
这里这个题目都是用抑或操作来处理数组里面的数据。
主要是下面这个公式
// 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 0
- 异或操作
- 异或操作总结
- 异或操作妙用
- 异或操作
- 与、或、异或的操作
- 二进制位 :或 异或 与 操作
- 异或操作的应用
- 异或操作求取最小值
- C++ 中的异或操作^
- Java中的异或操作
- 按位与(&)、或(|)、异或(^)等操作符
- 按位与、或、异或等的相关操作
- 异或操作符的妙用
- 位操作应用——异或
- C语言异或操作的妙用
- 高效实用的异或操作
- C语言异或操作的妙用
- C语言异或操作的妙用
- 647. Palindromic Substrings
- (三)、Java复习笔记之常见对象
- [学习随笔]并发编程与多线程二
- Oracle事物
- 自考总结
- 异或操作
- 经验总结-SwipeRefreshLayout控件的简单使用
- 写个程序判断系统是多少位,是大端还是小端
- Python基础知识学习
- 【转】阿里、华为、腾讯Java技术面试题精选
- 【JZOJ 5433】【NOIP2017提高A组集训10.28】图
- 解决 javaweb下载文件时报 您的主机中的软件中止了一个已建立的连接
- 【JZOJ5428】【NOIP2017提高A组集训10.27】查询
- 线程笔记1——多线程概述