[恢]hdu 2095

2011-12-27 05:25:53

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2095

题意:输入n(1000000)个数字,其中只有一个出现了奇数次。问是哪个。

mark:这题入选了M67大牛所说的最巧妙的算法题之一。方法是XOR起来。因为a^b^a=b。

代码:

# include <stdio.h>


int main ()
{
int n, num, ans ;
while (~scanf ("%d", &n) && n)
{
ans = 0 ;
while (n--)
{
scanf ("%d", &num) ;
ans ^= num ;
}
printf ("%d\n", ans) ;
}
return 0 ;
}



原文地址:https://www.cnblogs.com/lzsz1212/p/2315376.html