[九度][何海涛] 数组中只出现一次的数字
2012-11-23 11:28
246 查看
题目描述:一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。输入:
每个测试案例包括两行:
第一行包含一个整数n,表示数组大小。2<=n <= 10^6。
第二行包含n个整数,表示数组元素,元素均为int。
输出:对应每个测试案例,输出数组中只出现一次的两个数。输出的数字从小到大的顺序。样例输入:
样例输出:
每个测试案例包括两行:
第一行包含一个整数n,表示数组大小。2<=n <= 10^6。
第二行包含n个整数,表示数组元素,元素均为int。
输出:对应每个测试案例,输出数组中只出现一次的两个数。输出的数字从小到大的顺序。样例输入:
8 2 4 3 6 3 2 5 5
样例输出:
4 6
#include <iostream> #include <cstdio> using namespace std; int a[1000000]; int main() { int n; while(scanf("%d", &n) != EOF) { int res = 0; for(int i = 0; i < n; i++) { scanf("%d", &a[i]); res ^= a[i]; } int mask = 1; while((mask & res) == 0) mask = mask << 1; int num1 = 0; int num2 = 0; for(int i = 0; i < n; i++) if ((a[i] & mask) == 0) num1 ^= a[i]; else num2 ^= a[i]; if (num1 > num2) { int t = num1; num1 = num2; num2 = t; } cout << num1 << ' ' << num2 << endl; } }
相关文章推荐
- 整形数组中只出现一次的数字(剑指Offer,九度OJ)
- [九度][何海涛] 数组中出现次数超过一半的数字
- 九度OJ 1351 数组中只出现一次的数字
- 九度笔记之 1351:数组中只出现一次的数字
- 剑指Offer - 九度1351 - 数组中只出现一次的数字
- 九度OJ 1351 数组中只出现一次的数字
- 题目1351:数组中只出现一次的数字-九度
- [九度oj]题目1351:数组中只出现一次的数字
- 【九度OJ1351】|【剑指offer40】数组中只出现一次的数字
- 九度 题目1351:数组中只出现一次的数字
- 九度 题目1351:数组中只出现一次的数字
- 九度OJ 1351 数组中只出现一次的数字 (位操作)
- 九度OJ-题目1351:数组中只出现一次的数字
- 【剑指offer】【九度oj】数组中只出现一次的数字
- 数组中只出现一次的两个数字
- [九度oj]题目1370:数组中出现次数超过一半的数字
- 【面试题】剑指Offer-40-数组中只出现一次的数字
- 数组中只出现一次的数字——位运算
- 程序员面试题精选100题(63)-数组中三个只出现一次的数字
- 数组中只出现一次的数字