Hdoj-2095-find your Present (2) "Bit XOR"

Source: Internet
Author: User

Find your present (2)Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 18431 Accepted Submission (s): 7101


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 ndYou 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.
Outputfor each case, output an integer with a line, which is the card number of your present.
Sample Input
51 1 3 2 231 2 10

Sample Output
HintHintuse scanf to avoid time Limit exceeded

Author8600
Sourcehdu 2007-spring Programming Contest-warm up (1)
Recommend8600 | We have carefully selected several similar problems for you:20941597 1593 1595 1599
originally thought this method must be timed out, did not expect to be able to, visible: bit operation efficiency is still very high
Exe.time Exe.memory Code Len.
889MS 5324K 506 B


#include <stdio.h> #include <algorithm>using namespace  std;int a[1000010];int main () {    int n;    while (scanf ("%d", &n), n) {        int i,k;        for (i=0;i<n;++i) {            scanf ("%d", &a[i]);        }        Sort (a,a+n);        k=0;        int ok=0;        for (i=1;i<n;++i) {            if (A[i]^a[k]) {                //printf ("%d,%d\n", A[i],a[k]);                printf ("i-k=%d\n", i-k);                if ((i-k) &1) {                    ok=1;                    printf ("%d\n", A[k]);                    break;                }                else{                    k=i        ;        }}} if (!ok) printf ("%d\n", a[n-1]);            }    return 0;}

originally did not think can be directly with the difference or processing, because the number of other cards are even, any two of the same number is different or after 0, and different or satisfy the Exchange law, the order of operations independent, the result is the same


Exe.time
Exe.memory Code Len.
748MS 1404K 187B

#include <stdio.h>int main () {    int n;    while (scanf ("%d", &n), n) {        int a,i,res=0;        for (i=0;i<n;++i) {            scanf ("%d", &a);            res=res^a;        }        printf ("%d\n", res);    }    return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj-2095-find your Present (2) "Bit XOR"

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.