• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 2095 find your present (2) (异或)

题意:给定n个数,让你找出那一个次数为1的。

析:由于题意说了,只有那一个数是奇数,所以其他的都是偶数,根据异或的性质,两个相同的数异或为0;

任何数和0异或得原数,可以很简单的做出这个题。

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <map>

using namespace std;

int main(){
    int n, x;
    while(~scanf("%d", &n) && n){
        scanf("%d", &x);
        int ans = x;
        for(int i = 1; i < n; ++i){
            scanf("%d", &x);
            ans ^= x;
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

posted on 2016-06-02 13:02  dwtfukgv  阅读(175)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3