zoukankan      html  css  js  c++  java
  • P1496 找筷子

    (↑限制空间4MB) 

    这是一道关于位运算的题目,这里介绍一些关于异或 的性质.

    1、交换律

    2、结合律(即(a^b)^c == a^(b^c))

    3、对于任何数x,都有x^x=0,x^0=x

    4、自反性 A ^ B ^ B = A ^  0 = A

    (转载于此)

    注意到A ^ B ^ B = A ^  0 = A,再根据交换律知如果把一个数列所有的数放在一起异或一下,那么最后得到的数就是数列中唯一的出现了奇数次的数.

    所以这题的实现非常简单:

    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int n, ans;
    
    inline int read() {
        char ch = getchar();
        int x = 0, f = 1;
        while (ch > '9' || ch < '0') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = x * 10 + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    int main() {
        n = read();
        for (int i = 1; i <= n; i++) ans ^= read();
    
        printf("%d
    ", ans);
    }
  • 相关阅读:
    Yarn
    easyui
    eclipse-android
    js-小技能 そうですか
    sql server 时间处理
    上传文件
    时间 & 时间戳 之间 转换
    JDIC
    Spring 定时器
    映射
  • 原文地址:https://www.cnblogs.com/Gaomez/p/14242560.html
Copyright © 2011-2022 走看看