Find a number

Find a number

时间限制: 5 Sec  内存限制: 2 MB
提交: 98  解决: 16
[ 提交][ 状态][ 讨论版]

题目描述

Find a number which is repeated odd times, then You should output the number.

Example 1:

if input is:12 12 12 12 15

then output is: 15

Example 2:

if input is:12 13 12 13 18 12 13 13 18

then output is: 12

输入

First line contains a positive integer N < 500000 ,then, N positive integers follow (delimited with space) each less than 1 000 000.

输出

In input sequence only one number X is repeated odd times. Others have even number of occurrences. You should output X.

样例输入

9
3 1 2 2 17 1 3 17 3

样例输出

3

提示

If you can avoid error "Memory Limit Exceed", this problem will be a very simple problem.

这个题的亮点在于异或的运用

一个数a两次异或同一个数,等于原来的数a。



#include<iostream>
 
using namespace std;
 
int main()
{
    int n;
    while(cin>>n)
    {
        int tmp;
        int s=0;
        while(n--)
        {
            cin>>tmp;
            s^=tmp;
        }
        cout<<s<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值