boj23 Easy problem

问题:

Description


There is N pairs of balls in a small box. That means the number for each pair is the same. However, for some reason, a ball is lost. Now, you will get the number of the rest. Can you find the number of the lost ball as fast as possible?

 

Input


The input consists of several test cases. For each case, the first line contains an integer N(1<= N <= 500000), the total number of toys in the field. Then a line follow, which contains 2*N-1 numbers, which will is positive and not exceed 2*10^9. The input is terminated by N=0.


Output


For each test case, print in one line the number of the lost ball.

 

Sample Input


3

2 4 5 4 5

5

199 342 199 341 332 340 332 342 340

0


Sample Output


2

341

 

 

思路:

 

对于如此多的输入数据(500000×2-1),进行全部存储是不可行的,部分存储也不现实,因为输入数据是随机顺序的。因此采用另一种思路,假设一种操作f,使得待求数xi = f(x1,x2,……xn)。加减乘除肯定不行,逻辑运算也不行,那么只剩下位运算,我们知道a^a(按位异或操作)=0,0^b=b,且按位异或满足交换律,那么,将所有输入数据按位进行异或操作,最后将得到待求数xi

 

代码:

 

#include <stdio.h>
int main(){
    unsigned long int n,p,q;
    while(1){
        scanf("%d", &n);
        if(n==0)
            break;
        p=0;
        for(int i=0;i<2*n-1;i++){
            scanf("%d",&q);
            p=p^q;
        }
        printf("%d\n",p);
    }
    return 0;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值