計蒜客————單獨的數字(二)
阿新 • • 發佈:2019-02-11
一個整型陣列中有一個元素只出現一次,其它元素都出現兩次。求出只出現一次的元素。
要求:
線性時間複雜度,不能使用額外空間。
聰明的你能搞定嗎?
格式:
第一行輸入數字n,代表有n個數,根據題意,很明顯n是奇數,
第二行輸入陣列A[i], i從0~n-1.
最後輸出單獨的數字。
樣例輸入
7 1 3 2 0 3 2 1 這道題的歸類是位運算,利用兩個相同的值異或為0,來求得唯一不同的值。
#include <iostream> #include<cstring> #include<stdio.h> #include<algorithm> #include<map> #define MAX 9223372036854775807 using namespace std; int main() { int n; while(~scanf("%d",&n)) { int com; scanf("%d",&com); n--; while(n--) { int t; cin>>t; com=com^t; } printf("%d\n",com); } return 0; }