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. MVC 检测用户是否登录

         当我们访问一个网站的需求检測用户是否已经登录(通过Session是否为null),我们知道在WebForm中能够定义一个BasePage类让他继承System.Web.UI.Page,重写它 ...

  2. [ACM] HDU 3395 Special Fish (最大重量二分图匹配,KM算法)

    Special Fish Problem Description There is a kind of special fish in the East Lake where is closed to ...

  3. Phone Number 2010年山东省第一届ACM大学生程序设计竞赛

    Phone Number Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that if a phone number A is anothe ...

  4. POJ 1798 Truck History

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  5. java提高篇(十一)-----代码块

    在编程过程中我们可能会遇到如下这种形式的程序: public class Test { { //// } } 这种形式的程序段我们将其称之为代码块,所谓代码块就是用大括号({})将多行代码封装在一起, ...

  6. HDU 4391 Paint The Wall 段树(水

    意甲冠军: 特定n多头排列.m操作 以下是各点的颜色 以下m一种操纵: 1 l r col 染色 2 l r col 问间隔col色点 == 通的操作+区间内最大最小颜色数的优化,感觉非常不科学... ...

  7. 由<a href = "#" > 引发的思考

    原文:由<a href = "#" > 引发的思考 前阵子在一个移动项目中,通过 <a href = "#" >  的方式 绑定clic ...

  8. ACdream: ACfun

    ACfun Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisti ...

  9. 【Android基础】点击Back键退出应用程序

    //第一种方法(弹出对话框) @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-genera ...

  10. unity3d c# 产生真正的随机数

    虽然能够使用Random类来生成随机数.但它是系统时钟种子,因此,有大量的反复产生伪随机数的. 您可以使用RNGCryptoServiceProvider();相对真随机数生成. 由加密服务提供程序( ...