zoukankan      html  css  js  c++  java
  • Fedor and New Game (异或运算)

    题目链接:http://codeforces.com/problemset/problem/467/B

    题目大意:有 m + 1 个 player 和 n 种类型的 soldiers。每个player被赋予一个数xi,然后将xi 看成二进制数,规定第 j 位 如果为1,表示这个 player 有j 这种类型的soldiers。Fedor 是 第 m + 1 个player,问他能跟前面 m 个players 成为 friends 的 人数。成为friends 的条件是被比较的两个人的不同soldiers数不得多于 k 个。

    思路:

    因为要统计不同的位数。所以想到异或运算,它可以把两者相同的部分变为0,不同的部分变为1

    然后我们再去统计1的个数就好了!

    至于如果去统计 1 的个数,我们可以考虑这个数和1进行按位与运算。

    第一次用这个感觉好神奇!

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <string>
     4 #include <iostream>
     5 #include <stdlib.h>
     6 #include <algorithm>
     7 
     8 using namespace std;
     9 const int maxn = 1000 + 10;
    10 int a[maxn];
    11 
    12 
    13 
    14 int main()
    15 {
    16 #ifndef ONLINE_JUDGE
    17     freopen("../in.txt","r",stdin);
    18 #endif
    19     int n, m, k;
    20     while (scanf("%d%d%d", &n, &m, &k) != EOF)
    21     {
    22         for (int i = 0; i < m; i++)
    23             scanf("%d", &a[i]);
    24         scanf("%d", &a[m]);
    25         int ans = 0;
    26         for (int i = 0; i < m; i++)
    27         {
    28             int tmp = a[i] ^ a[m];
    29             int cnt = 0;
    30             for (; tmp; tmp >>= 1)
    31             {
    32                 if (tmp & 1)
    33                     cnt++;
    34             }
    35             if (cnt <= k)
    36                 ans++;
    37         }
    38         printf("%d
    ", ans);
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    导入.txt数据
    struts1与struts2
    居中问题
    JavaScript创建对象的几种方式
    json
    react模拟后端接口及提交方式
    react受控组件
    react中数据承载props和state用法
    react组件间的通信父子通信,子父通信,兄弟通信
    react模拟后端接口及提交方式 jsonserver
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11210747.html
Copyright © 2011-2022 走看看