In the new year party, everybody will 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 them will be yours.Each present has a card number on it, and your present's card number will be the one that different from all the others, and you can assume that only one number appear odd times.For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present will be the one with the card number of 3, because 3 is the number that different from all the others.

InputThe input file will consist of several cases. 
Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.OutputFor each case, output an integer in a line, which is the card number of your present.Sample Input

  1. 5
  2. 1 1 3 2 2
  3. 3
  4. 1 2 1
  5. 0

Sample Output

  1. 3
  2. 2
  3.  
  4. use scanf to avoid Time Limit Exceeded

Hint

  1. Hint
  2.  
  3. 题意:
      给出n,说明有n个数字,要求找出一个与其他数字不同的那个数,只有需要找的那个数字出现过奇数次,其他都是偶数次。
    法一:
    这是用容器写的,用了set
    s.find(x)!=s.end() 说明找到了
    s.erase(x); 找到和它一样的就把它删掉
    s.insert(x); 没有找到就把这个数字插入
    需要注意的是:题目说了,找到的那个数字是奇数个,而其他数字都是偶数个,所以可以利用set,否则要是我们需要找3,给出一组数据2223像这样是找不出来的。
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<string.h>
  4. #include<set>
  5. #define inf 0x3f3f3f3f
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. std::ios::sync_with_stdio(false);
  11. cin.tie();
  12. cout.tie();
  13. int n;
  14. while(cin>>n)
  15. {
  16. if(n==)
  17. break;
  18. set<int>s;
  19. s.clear();
  20. int x;
  21. for(int i=;i<n;i++)
  22. {
  23. cin>>x;
  24. if(s.find(x)!=s.end())//找到了
  25. s.erase(x);//删掉
  26. else//没有找到
  27. s.insert(x);
  28. }
  29. cout<<*s.begin()<<endl;
  30. }
  31. return ;
  32. }
  1. 法二:
    利用位运算
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<string.h>
  4. #include<set>
  5. #define inf 0x3f3f3f3f
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. std::ios::sync_with_stdio(false);
  11. cin.tie();
  12. cout.tie();
  13. int n;
  14. while(cin>>n)
  15. {
  16. if(n==)
  17. break;
  18. int x=;//0和任何非0数m异或都为0;
  19. for(int i=;i<n;i++)
  20. {
  21. int ss;
  22. cin>>ss;
  23. x=x^ss;
  24. }
  25. cout<<x<<endl;
  26. }
  27. return ;
  28. }
  1.  
  1.  

HDU-2095-find your present (2)-位或/STL(set)的更多相关文章

  1. HDU 2095 find your present (2)

    HDU 2095 find your present (2) 解法一:使用set 利用set,由于只有一个为奇数次,对一个数进行查询,不在集合中则插入,在集合中则删除,最后剩下的就是结果 /* HDU ...

  2. hdu 2095 find your present (2) 位运算

    题意:找出现次数为奇数的数 xor运算 #include <cstdio> #include <iostream> #include <algorithm> usi ...

  3. HDU 2095 find your present (2) (异或)

    题意:给定n个数,让你找出那一个次数为1的. 析:由于题意说了,只有那一个数是奇数,所以其他的都是偶数,根据异或的性质,两个相同的数异或为0: 任何数和0异或得原数,可以很简单的做出这个题. 代码如下 ...

  4. HDU 2095 find your present (2) 动态链表

    解题报告:输入一个n,后面紧跟着输入n个数,输入的这n个数中,除了有一个数的个数为奇数外,其它的数的个数都是偶数个,现在要你找出这个个数为奇数的这个数. 看起来好像很简单的样子,不过,这题的重点不在这 ...

  5. HDU 2095 find your present (2)( 位运算 )

    链接:传送门 题意:给出n个数,这n个数中只有一种数出现奇数次,其他全部出现偶数次,让你找到奇数次这个数 思路:简单异或运算题 /*********************************** ...

  6. HDU.2095(异或运算)

    find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  7. HDU 1004 Let the Balloon Rise【STL<map>】

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. 杭电 2095 find your present (2)【位运算 异或】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2095 解题思路:因为只有我们要求的那个数出现的次数为奇数,所以可以用位运算来做,两次异或同一个数最后结 ...

  9. hdu 1563 Find your present!

    Find your present! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. Centos 安装php Imagick 扩展

    从 centos 仓库安装 首先安装 php-pear php-devel,gcc三个软件包 yum install php-pear php-devel gcc 通过 yum 安装Centos 官方 ...

  2. WPF 模仿 UltraEdit 文件查看器系列 开篇和导读

    WPF 模仿 UltraEdit 文件查看器系列 开篇和导读 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 学 .Net FrameW ...

  3. 二分法的应用:最大化最小值 POJ2456 Aggressive cows

    /* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  4. 阿里云POLARDB如何帮助百胜软件应对数据库的“巅峰时刻”

    POLARDB是阿里云自研的下一代关系型云数据库,100%兼容MySQL,存储容量最高可达100TB,性能最高提升至MySQL的6倍,适用于企业多样化的数据库应用场景.POLARDB采用存储和计算分离 ...

  5. 【Codeforces Round #589 (Div. 2) D】Complete Tripartite

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实这道题感觉有点狗. 思路大概是这样 先让所有的点都在1集合中. 然后随便选一个点x,访问它的出度y 显然tag[y]=2 因为和他相连了嘛 ...

  6. bzoj1009题解

    [解题思路] 先KMP出fail数组,再用fail数组求出M[i][j],表示上一次匹配到第i位,这次可以遇到多少种不同的字符,使之转而匹配到第j位. 设集合S=[1,m]∩N 又设f[i][j]表示 ...

  7. spark在collect收集数据的时候出现outOfMemoryError:java heap space

    spark的collect是action算子,所有最后会以数组的形式返回给driver端,当数据太大的时候就会出现堆内存溢出.OutofMemoryError:java heap space. 在sp ...

  8. function attributes, MDK

    The keyword format is either of the following: __attribute__((attribute1, attribute2, ...)) __attrib ...

  9. 文本数据和mysql 里面的数据比较

    实现读取TXT文件中的内容然后存到内存,然后将内存中的数据和mysql 数据库里面某张表数据的字段做一个比较,如果比较内存中的数据在mysql 里存在则不做处理,如果不存在则将该数据插入mysql数据 ...

  10. Redis事务,持久化,哨兵机制

    1 Redis事务 基本事务指令 Redis提供了一定的事务支持,可以保证一组操作原子执行不被打断,但是如果执行中出现错误,事务不能回滚,Redis未提供回滚支持. multi 开启事务 exec 执 ...