zoukankan      html  css  js  c++  java
  • Gym 100818I Olympic Parade(位运算)

    Olympic Parade

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=101594#problem/I

    【题意】:

    给出N个数,找出没有恰好出现K次的那个数.

    【解题思路】:

    题目卡了一下内存,不能太暴力...

    然而还是很暴力,排个序再遍历就好.

    也可以用位运算写成只要4KB:

    把所有数字的二进制位都累加起来(就是算第i位一共出现了几次1)

    如果某一位上1的数目不是k的倍数,那么要找的那个数在这一位必定为1,否则为0。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #define LL long long
     7 #define maxn 1100000
     8 #define IN freopen("in.txt","r",stdin);
     9 using namespace std;
    10 
    11 int n,k;
    12 int bits[40];
    13 
    14 int main(int argc, char const *argv[])
    15 {
    16     //IN;
    17 
    18     while(scanf("%d %d",&n,&k)!=EOF)
    19     {
    20         memset(bits,0,sizeof(bits));
    21 
    22         for(int i=1; i<=n; i++){
    23             int x;scanf("%d",&x);
    24             int j = 0;
    25             while(x!=0){
    26                 bits[j] += x%2;
    27                 j++;
    28                 x /= 2;
    29             }
    30         }
    31 
    32         int ans = 0;
    33         for(int i=0; i<=32; i++){
    34             int tmp = bits[i];
    35             if(tmp%k == 0) continue;
    36             ans += (1<<i);
    37         }
    38 
    39         printf("%d
    ", ans);
    40     }
    41 
    42     return 0;
    43 }
  • 相关阅读:
    c#的Marshal
    爬虫之requests详解
    爬取抖音视频
    爬取拉钩网
    爬虫自动登陆GitHub
    爬取博客园博客
    爬取煎蛋网文章
    爬取抽屉热搜榜文章
    准备
    爬虫示例
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5031591.html
Copyright © 2011-2022 走看看