码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2095 find your present (2) 异或

时间:2018-01-19 23:30:58      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:net   std   hid   alt   you   play   pen   解题报告   技术   

异或

 

暴力开数组,然而明显不过,要求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 (2) 异或

标签:net   std   hid   alt   you   play   pen   解题报告   技术   

原文地址:https://www.cnblogs.com/berred/p/8318970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!