Nyoj question 528 find the ball number (3)

Source: Internet
Author: User
Ball search (3) time limit: 2000 MS | memory limit: 3000 KB difficulty: 2
Description

Xiaod is now responsible for tennis management at a certain Stadium. To facilitate management, he coded every ball and the total number of balls for each number was an even number. One day, xiaod found that one ball was missing. Could you help him find the lost ball number?

Input
There are multiple groups of test data. Each group of data includes two rows.
The first row is an integer N (0 <n <1000000), indicating the number of remaining balls.

The next row contains N numbers, indicating the number M (0 <m <10 ^ 9) of the remaining balls ).
Output
For each set of data, output the ball number of the lost ball.
Sample Input
 
51 1 3 6 631 2 1

Sample output

 

 
32

At first, I did not pay attention to the memory limit. I opened an array and recorded the ball number with the array subscript. In the final query, I used a [I] To get the remainder of 2. When the remainder is 1, output I. The overmemory is found only after submission.

 

Super MemoryCode:

 

 
# Include <stdio. h> # include <string. h> int A [1000002]; int main () {int N, I, m, Max; while (~ Scanf ("% d", & N) {memset (A, 0, sizeof (a); max = 0; for (I = 0; I <N; I ++) {scanf ("% d", & M); A [m] ++; If (M> MAX) max = m ;}for (I = 1; I <= max; I ++) if (a [I] & 1) {printf ("% d \ n", I); break ;}} return 0 ;}

 

Later, I heard people say that they can use an exclusive or nature for processing. I checked the exclusive or nature and wrote the code and submitted it to the AC.

Operation Rule for XOR: 0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 1 = 0

Specifically, convert the decimal number to the binary number (take 8 or 16 bits, and fill in 0 if not enough), and then each bit corresponds to an exclusive or operation, converting the result to a decimal number is an exclusive or result. For example, 9 ^ 5 = 12;

00001001 (binary representation of 9)

00000101 (binary representation of 5)

00001100 (the result after XOR is expressed as 12 in decimal format)

The Code is as follows:

 

 
# Include <stdio. h> int main () {int n, m, I, S; while (~ Scanf ("% d", & N) {S = 0; for (I = 0; I <n; I ++) {scanf ("% d ", & M); s ^ = m;} printf ("% d \ n", S);} return 0 ;}

 

Another method is to use the container in C ++ for processing. I think someone else wrote this as a reference. The Code is as follows:

 

# Include <stdio. h ># include <set> using namespace STD; int main () {int N, A, B, I; set <int> T; // define an int-type container while (scanf ("% d", & N )! = EOF) {B = 0; for (I = 0; I <n; I ++) {scanf ("% d", & A); If (T. find (A) = T. end () T. insert (a); // The container does not have the same content as a. Insert a else t. erase (a); // locate and delete all a} printf ("% d \ n", * t. begin (); T. clear (); // clear the container} return 0 ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.