HDOJ(HDU) 1563 Find your present!(异或)

简介: HDOJ(HDU) 1563 Find your present!(异或)

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.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<=200, and n is odd) at first. Following that, n positive integers will be given in a line. 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


题意:找出一行数中独立的数!


按位异或的3个特点:

(1) 0^0=0,0^1=1 0异或任何数=任何数

(2) 1^0=1,1^1=0 1异或任何数-任何数取反

(3) 任何数异或自己=把自己置0


先说一下异或运算的运算法则:

1. a ^ b = b ^ a

2. a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c

3. d = a ^ b ^ c 可以推出 a = d ^ b ^ c

4. a ^ b ^ a = b


对于性质1,显而易见。

对于性质2和4,就是可以查找出一组数列中具有奇数个数的数。比如:

题目:有2n+1个数,其中有n个数出现过两次,只有一个数字出现过一次。要求是找出这个数字。


import java.util.Scanner;


public class Main{

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);


       while(sc.hasNext()){

           int n =sc.nextInt();

           if(n==0){

               return ;

           }

           int m =0;

           int s;

           while(n-->0){

               s = sc.nextInt();

               m = m^s;

           }

           System.out.println(m);

       }

   }

}


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22


程序编程之旅


微信公众号


编程之旅,踏人生之路,观世界之行


与50位技术专家面对面


20年技术见证,附赠技术全景图


谙忆

关注


1



2


0



专栏目录






逸川先生

2017.11.14


学长,我拿你的代码去还是超时了,怎么回事


丁国华

2016.04.19


感谢博主的分享。


谙忆作者

回复

丁国华

2016.04.19


客气了(^-^)

相关推荐


hdu-1563 Find your present!

Just do IT!

629

关于寻找数组中的特别数字的小技巧 Find your present! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2335    Accepted Submission(s): 1524 Problem

HDU 1563 Find your present! 异或妙用找到独立出现的数

最新发布

xiongshuxian2019的博客

54

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 p

HDU 1563 Find your present! 找出不同的数 异或 - 小...

10-13

and your present's card number will be the one that different from all the others.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 nu...

hdoj 1563 Find your present!&&2095 Find your present(2) 异或...

3-16

&&2095 Find your present(2) 异或运算    http://acm.hdu.edu.cn/showproblem.php?pid=1563 Find your present! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)...

D - find your present (2)

LMengi000的博客

253

D - find your present (2) 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

find your present

杨的博客

202

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...

【基础训练-异或的运用】find your present_It's the C...

3-9

【基础训练-异或的运用】find your present 问题链接:HDU1563 Find your present!,HDU2095 find your present(2)。 问题简述:(略) 问题分析:For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1....

hdu-oj 1563 Find your present!_MatchstickMen的博客

3-29

hdu-oj 1563 Find your present! Find your present! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2690 Accepted Submission(s): 1785...

HDU 2095 find your present (2)

可可 の 猫

435

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

hdoj problem 1563 Find your present!(位运算(异或运算求特殊数))

亿念之茶的专栏

368

Find your present! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2803    Accepted Submission(s): 1854 Problem Description In the ne

hdu 1563

ysc504的专栏

325

Find your present! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1716    Accepted Submission(s): 1111 Problem Description In the new

杭电OJ-- 2095 find your present (依然很水)

李树花开,风中摇曳

1596

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 wil

【ACM解题报告】Find Your Present 2

Bill好想

510

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

OJ-HDU find your present (2)

the blog of Lee

219

                          find your present (2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28605    Accepted Submission(s): 11214Problem...

HDU-2095 find your present (异或运算)

Sclong0218的博客

112

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

hdoj 2095 find your present (2)【位运算,异或】

nailnehc的博客

284

find your present (2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18608    Accepted Submission(s): 7167 题目大意:给你n个数,找出其中唯一一个出现过奇数次的

HDU1563 HDU2095 Find your present!【密码】

海岛Blog

1628

Find your present! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4422    Accepted Submission(s): 2922 Problem Description In the new

杭电 HDU ACM 1563 Find your present!

舒哥的blog

965

Find your present! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3096    Accepted Submission(s): 2051 Problem Description In the new

find your present (2)

逆水行舟 不进则退

840

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

HDOJ-1563-Find your present! 解题报告

JZQT_T的博客

743

位运算巧解题。题意:给你n个数,n为奇数,这n个数里面除了一个数只出现一次其他数都有出现两次,请输出那个出现一次的数。        我的解题思路:根据异或运算性质,相同的数异或等于0,0与任何数异或都得任何数,异或运算满足交换律。这么以来把这所有的数都异或之后就得到了只出现一次的数了。        我的解题代码: #include #include #include

HDU 1563 【Find your present!】

weixin_30753873的博客

29

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 ...

Find your present!(杭电1563)

My Castle

583

Find your present! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2742    Accepted Submission(s): 1815 Problem Description In the

“相关推荐”对你有帮助么?


非常没帮助


没帮助


一般


有帮助


非常有帮助

©️2022 CSDN 皮肤主题:代码科技 设计师:Amelia_0503 返回首页

关于我们

招贤纳士

商务合作

寻求报道


400-660-0108


kefu@csdn.net


在线客服

工作时间 8:30-22:00

公安备案号11010502030143

京ICP备19004658号

京网文〔2020〕1039-165号

经营性网站备案信息

北京互联网违法和不良信息举报中心

家长监护

网络110报警服务

中国互联网举报中心

Chrome商店下载

©1999-2022北京创新乐知网络技术有限公司

版权与免责声明

版权申诉

出版物许可证

营业执照


谙忆



码龄7年

Java领域优质创作者

1033

原创

4654

周排名

1336

总排名

443万+

访问

等级

4万+

积分

1万+

粉丝

1952

获赞

2173

评论

1909

收藏


















私信

关注


友情栏目


GitHub


QQ技术交流群:819539788

联系邮箱:uifuture@uifuture.com

微信公众号:程序编程之旅

编程之旅,人生之路,不止于编程,还有诗和远方。

阅代码原理,看框架知识,学企业实践;

赏诗词,读日记,踏人生之路,观世界之行;

另:博客中的大部分收费文章,在公众号免费看。关注后有技术交流群加入方式


友情链接

技术数据整合

IT数据的整合

Java技术  

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n =sc.nextInt();
            if(n==0){
                return ;
            }
            int m =0;
            int s;
            while(n-->0){
                s = sc.nextInt();
                m = m^s;
            }
            System.out.println(m);
        }
    }
}
目录
相关文章
|
机器学习/深度学习 Java
HDOJ 2095 find your present (2)
HDOJ 2095 find your present (2)
114 0
HDOJ 2095 find your present (2)
HDOJ 1016 Prime Ring Problem素数环【深搜】
HDOJ 1016 Prime Ring Problem素数环【深搜】
121 0
HDOJ 1016 Prime Ring Problem素数环【深搜】
HDU-1598,find the most comfortable road(并查集+枚举)
HDU-1598,find the most comfortable road(并查集+枚举)
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
122 0
HDOJ(HDU) 2162 Add ‘em(求和)
HDOJ(HDU) 2162 Add ‘em(求和)
77 0
HDOJ(HDU) 1678 Shopaholic
HDOJ(HDU) 1678 Shopaholic
111 0
|
Java
HDOJ/HDU 1297 Children’s Queue(推导~大数)
HDOJ/HDU 1297 Children’s Queue(推导~大数)
144 0
|
Java
HDOJ(HDU) 1720 A+B Coming(进制)
HDOJ(HDU) 1720 A+B Coming(进制)
121 0
|
存储
HDOJ/HDU 1073 Online Judge(字符串处理~)
HDOJ/HDU 1073 Online Judge(字符串处理~)
103 0
HDOJ/HDU 2700 Parity(奇偶判断~)
HDOJ/HDU 2700 Parity(奇偶判断~)
144 0