HDU 2095 find your present 异或

Posted berred

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2095 find your present 异或相关的知识,希望对你有一定的参考价值。

异或

 

暴力开数组,然而明显不过,要求32768k,结果超时了

技术分享图片
#include <cstdio>
#include <cstring>
int book[1000000];
int main()
{
    int n;
    while (scanf("%d", &n) && n)
    {
        int a;
        memset(book, 0, sizeof(book));
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a);
            book[a] = !book[a];
        }
        for (int i = 1; i < n; i++)
            if (book[i])
            {
                printf("%d\n", i);
                break;
            }
    }
    return 0;
}
View Code

再试试投机的一个办法,直接对n/2+1,这要看test了,结果wronganswer,也是预料中的结果

技术分享图片
#include <cstdio>
int main()
{
    int n;
    while (scanf("%d", &n) && n)
    {
        int a;
        for(int i=0; i<n; i++)
            scanf("%d", &a);
        printf("%d\n", n / 2 + 1);
    }
    return 0;
}
View Code

通过代码

#include <cstdio>
int main()
{
    int n;
    while (scanf("%d", &n) && n)
    {
        int a = 0, b;
        while (n--)
        {
            scanf("%d", &b);
            a ^= b;
        }
        printf("%d\n", a);
    }
    return 0;
}

 

参考网上的解题报告 http://blog.csdn.net/dgq8211/article/details/7455722

目前看到三种方法,异或(似乎最为简洁)、STL的map(减小内存)、最大值(谜?)

可以试试

 

以上是关于HDU 2095 find your present 异或的主要内容,如果未能解决你的问题,请参考以下文章