NYOJ528 Finding the ball number (three) bit arithmetic

Source: Internet
Author: User

The problem with bit arithmetic is very simple, the premise is to first familiar with the bit operation, here to use an XOR operation, that is, ^ this symbol, his operation rule is: the same is 0, the difference is 1. After knowing this, it is easy to think of the same two number of different or after 0, so the following is a key step, but also I think for a long time did not think of a step, is to put all the numbers are different or once again, then the last remaining must be that one, there is a point to note that any number and 0 of the difference or operation is still himself Here's the code:

1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 intMain ()7 {8     intN;9      while(~SCANF ("%d", &N))Ten     { One         intAns =0;//Initialize Condition A         intT; -          for(inti =0; I < n; i++) -         { thescanf"%d", &t); -Ans ^= t;//XOR or Operation -         } -printf"%d\n", ans); +  -     } +     return 0; A}

There is also a code to look at the STL from the Internet as follows:

#include <iostream>#include<cstdio>#include<Set>using namespacestd;intMain () {intN;  while(~SCANF ("%d", &N)) {Set<int>S; intT;  for(inti =0; I < n; i++) {scanf ("%d", &t); if(S.find (t) = =S.end ())            S.insert (t); Elses.erase (t); } printf ("%d\n", *S.begin ()); }    return 0;}

Although the STL code time is relatively long, but this is also a good method, flexible use of STL is very necessary for ACM!

NYOJ528 Finding the ball number (three) bit arithmetic

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.