1. 程式人生 > >C語言:find a number

C語言:find a number

題目描述

Find a number which is repeated odd times, then You should output the number.
Example 1:
if input is:12 12 12 12 15
then output is: 15
Example 2:
if input is:12 13 12 13 18 12 13 13 18
then output is: 12
輸入
First line contains a positive integer N < 500000 ,then, N positive integers follow (delimited with space) each less than 1 000 000.
輸出
In input sequence only one number X is repeated odd times. Others have even number of occurrences. You should output X.
樣例輸入
9
3 1 2 2 17 1 3 17 3
樣例輸出
3

大佬寫:

#include<stdio.h>

int main()
{
	int a,n;
	int num;
	scanf("%d %d",&n,&num);
	a = num;
	int i;
	for(i = 1; i < n; i ++)
	{
		scanf("%d",&num);
		a ^= num;
	}
	printf("%d\n",a);
}

雖然知道位異或是怎麼一回事,但是完全不會用,現在長見識了,我自己寫的時候用的是陣列,但是時間超限了,等我問了老師,在補充上來。