HDOJfind your present (思路题)

Posted som_nico

tags:

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

find your present (2)

题意:找有唯一一个出现奇数次的数

思路:利用位异或。一个数异或自己本身是0;

#include<stdio.h>
int main()
{
    int n,i,x,y;
    while(scanf("%d",&n)!=EOF&&n)
    {
           x=0;
           while(n--)
           {
                scanf("%d",&y);
                x^=y;
           }
           printf("%d\n",x);
    }
    return 0;
}

 

 

map

#include <stdio.h>  
#include <algorithm>  
#include <map>  
using namespace std;  
  
int main()  
{  
    int n,i,a;  
    map<int,int>my;  
    map<int,int>::iterator it;  
    while(scanf("%d",&n),n)  
    {  
        for(i=0;i<n;i++)   
        {  
            scanf("%d",&a);  
            my[a]++;  
        }  
        for(it=my.begin();it!=my.end();it++)  
        {  
            if(it->second==1)  
            {  
                printf("%d\n",it->first);  
                break;  
            }  
        }  
        my.clear();  
    }  
    return 0;  
}  

 

以上是关于HDOJfind your present (思路题)的主要内容,如果未能解决你的问题,请参考以下文章

find your present

HDU 2095 find your present

7K - find your present

hdu 1563 Find your present!

HDU-2095-find your present -位或/STL(set)

find your present (感叹一下位运算的神奇)