[轉]數組中只出現一次的兩個數字
阿新 • • 發佈:2019-04-15
getc blog pri cst ons swap 出現一次 include \n
參考鏈接:https://www.cnblogs.com/yyxayz/p/4067529.html
示例代碼:
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; const int maxn = 5000050; // 快讀 inline int in() { char ch; int a=0; while(!(((ch=getchar())>='0')&&(ch<='9'))); //利用getchar讀入,速度快。 a*=10;a+=ch-'0'; while(((ch=getchar())>='0')&&(ch<='9'))a*=10,a+=ch-'0'; //而後用ASCII碼轉為int 類型 return a; } int n, a[maxn], b[maxn], nb = 0, c[maxn], nc = 0; int main() { n = in(); for (int i = 0; i < n; i ++) a[i] = in(); int t = 0; for (int i = 0; i < n; i ++) t ^= a[i]; int p = 1; while (!(t&p)) p <<= 1; for (int i = 0; i < n; i ++) { if (a[i] & p) b[nb++] = a[i]; else c[nc++] = a[i]; } int t1 = 0; for (int i = 0; i < nb; i ++) t1 ^= b[i]; int t2 = 0; for (int i = 0; i < nc; i ++) t2 ^= c[i]; if (t1 > t2) swap(t1, t2); printf("%d %d\n", t1, t2); return 0; }
[轉]數組中只出現一次的兩個數字