1. 程式人生 > >HDU 2095 find your present (2) 異或

HDU 2095 find your present (2) 異或

然而 open pre aps play 直接 ring clas div

異或

暴力開數組,然而明顯不過,要求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) 異或