Problem DescriptionIn The New Year party, everybody'll get a "special present". Now it's your turn to get your special present, a lot of presents now putting on the desk, and only one of the them would be yo Urs. Each present have a card number on it, and your present ' s card number would be the one that different from all the others, a nd you can assume this only one number appear odd times. For example, there is 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present would be is the one with the card n Umber of 3, because 3 is the number, which different from all the others. |
Inputthe input file would consist of several cases. Each case would be presented by an integer n (1<=n<1000000, and n are odd) at first. Following that, n positive integers would be given in a line, all integers would smaller than 2^31. These numbers indicate the card numbers of the PRESENTS.N = 0 ends the input. |
Output for each case, output an integer with a line, which is the card number of your present. |
Sample Input51 1 3 2 231 2 10 |
Sample Output32 Hint HintUse scanf to avoid time Limit exceeded |
Author8600 |
Sourcehdu 2007-spring Programming Contest-warm up (1) |
Recommend8600 |
Topic memory Limit: 1024K, so you cannot simply use the array to save and then process.
There is a good way to do this-bit XOR or.
A bitwise XOR algorithm:
1, a^b = B^a.
2, (a^b) ^c = a^ (b^c).
3, a^b^a = B.
For an arbitrary number n, it has several special properties:
1, 0^n = N.
2, n^n = 0.
So you can pass each XOR operation, and the last remaining value is the number that appears odd number of times.
#include <stdio.h>
int main ()
{
int N,x,ans;
while (scanf ("%d", &n), N)
{
Ans = 0;
while (n--)
{
scanf ("%d", &x);
Ans ^= x;
}
printf ("%d\n", ans);
}
return 0;
}
Find your Present (2) (Bit XOR)