Problem Description
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.
 
Input
The 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.
 
Output
For each case, output an integer in a line, which is the card number of your present.
 
Sample Input
5
1 1 3 2 2
3
1 2 1
0
 
Sample Output
3
2

Hint

Hint

use scanf to avoid Time Limit Exceeded

 
Author
8600
 
Source
 
Recommend
8600   |   We have carefully selected several similar problems for you:  2094 1008 1597 1593 1595 
 
 
 
 
 
 
 //map
#include <set>
#include <stdio.h>
using namespace std;
int main()
{
int n,x;
set <int> S;
while(scanf("%d",&n),n)
{
while(n--)
{
scanf("%d",&x);
if(S.find(x) == S.end()) //没找到,插入
S.insert(x);
else //找到了,删除
S.erase(x);
}
printf("%d\n",*S.begin());
S.clear();
}
return ;
}
 //排序

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstdlib>
using namespace std; long long int a[]; int main()
{
int n;
while (scanf("%d",&n) && n)
{
for (int i = ; i<n; i++)
scanf("%lld",&a[i]);
sort(a, a+n);
int i;
for (i = ; i<n; i++)
{
if (a[i] != a[i-] && a[i] != a[i+])
break;
}
printf("%lld\n",a[i]);
}
return ;
}
 #include <math.h>
#include <string.h>
#include <algorithm>
#include <stdio.h>
long a[];
int main()
{ memset(a,,sizeof(a));
long n;
while(scanf("%ld",&n)!=EOF&&n!=)
{
long long m;
for(int i=;i<n;i++)
{
scanf("%lld",&m);
a[m]+=;
}
for(int i=;i<;i++)
{
if(a[i]==)
{
printf("%lld\n",i);
}
}
}
return ;
}

其实,这题还有个更好的方法————位异或。

我们先了解一下位异或的运算法则吧:

1、a^b = b^a。

2、(a^b)^c = a^(b^c)。

3、a^b^a = b。

对于一个任意一个数n,它有几个特殊的性质:

1、0^n = n。

2、n^n = 0。

所以可以通过每次异或运算,最后剩下的值就是出现奇数次的那个数字。

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

find your present (2) 2095的更多相关文章

  1. HDU 2095 find your present (2)

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

  2. find your present (2) hdoj 2095

    /* author:谦智 find your present (2) hdoj 2095 法一:用暴力 法二:用map 法三: 符号是^. 异或是个位运算符号,具体是怎么操作的请百度,这里有个特性使得 ...

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

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

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

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

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

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

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

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

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

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

  8. HDOJ 2095

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

  9. HDU.2095(异或运算)

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

随机推荐

  1. Hadoop读书笔记(二)HDFS的shell操作

    Hadoop读书笔记(一)Hadoop介绍:http://blog.csdn.net/caicongyang/article/details/39898629 1.shell操作 1.1全部的HDFS ...

  2. Android 设计模式模式适配器

    自定义适配器模式:一类的接口,转换成客户的期望,也是一个接口.适配器使原本接口不是与类兼容可以无缝.下面两个图看起来更加清晰 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  3. Apache+Tomcat部署负载均衡(或集群)

    本来只打算写Tomcat集群部署,简化Apache和Tomcat整合过程的.后来想了想,这样不便于没有用过Apache的朋友来学习本文内容.于是干脆加大篇幅,让对Apache不了解的朋友能对Apach ...

  4. Mybatis深入之事务管理

    Mybatis之事务管理 简单介绍 Mybatis的事务管理分为两种JdbcTransaction.ManagedTransaction. 当中JdbcTransaction仅仅是对数据库连接Conn ...

  5. 深入浅出JMS(一)——JMS简要

    假设手机只能实时通话.没有邮件和短信功能发生?一个电话回来.只是没有足够的时间去连接.然后传递这款手机的信息肯定是不接受. 么不能先将信息存下来.当用户须要查看信息的时候再去获得信息呢?伴随着这个疑惑 ...

  6. ASP.NET MVC 5 入门摘要

    翻译和编辑的第一阶段后,.我们已经发出12片ASP.NET MVC 5入门文章. 他们中的大多数来自翻译ASP.NET MVC 5 官方教程,因为本系列文章言简意赅,篇幅适中,从一个web站点演示样例 ...

  7. 39一个可以让你受益HTML5课程

    事不宜迟,本文作者是我们收集的在线学习HTML5资源.我们希望他们能够帮助你更好地学习HTML5. 不错啊! 只,说原作者40一个只有39一.由于第一5钍和8一是重复. 原文在此! 1. 五分钟入门H ...

  8. MMTool制作Ozmosis引导BIOS完美引导OS X系统

    Ozmosis引导是德国黑苹果爱好者制作的一个引导程序,目前仍处于测试版,有了它,你可以不用再使用四叶草.变色龙之类引导工具,相对而言它更象白苹果.Ozmosis是基于AMI公司bios的硬件引导驱动 ...

  9. Jvascript方法

    Jvascript实用方法   这篇我主要记录一些在工作中常用的.实用的方法. String trim 字符串方法中的trim主要用来去空格使用,很多时候,在后台做参数处理的时候,我们都会使用该方法, ...

  10. C++生产和使用的临时对象

     所谓暂时对象就是一种无名对象. 它的出现假设不在程序猿的预期之下(比如不论什么pass by value操作都会引发copy操作,于是形成一个暂时对象),往往照成效率上的负担. 但有时候能够制造 ...