foj 1001 Duplicate Pair【简单位运算】

http://acm.fzu.edu.cn/problem.php?pid=1001

题目大意;找到1~n-1一共n个数中出现过两次的那个数。

    先预处理,异或操作,假设所有的数都出现了一次;然后再依次异或这n个数,结果就是所有出现过奇数次的数的异或和,而只有答案要求的那个数(加上预处理)是出现奇数次的,所以结果就是所要的答案。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
int a[1000100];
void init() {
    for(int i=1;i<=1000000;i++) a[i] = a[i-1]^i;
}
int main() {
    int n , aa;
    init();
    while(~scanf("%d",&n)) {
        int ans = a[n-1];
        while(n--) {
            scanf("%d",&aa);
            ans ^= aa;
        }
        printf("%d\n" , ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/aiiYuu/archive/2013/04/07/3003305.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值