牛客 乐视2017暑期实习生笔试题(二)基础位运算

今天的计算机课上,老师给同学们出了一道题:
输入n个数,请将其中的偶数的二进制反转。
eg:输入1 6 5
其中6是偶数,二进制表示为110,反转后为011,代表3,所以最终输出1 3 5.
小贱君最近脑子不怎么好使,想了半天也没想出来如何做,最后他向你寻求帮助了,帮帮可怜的小贱君吧!


题意,把所有有数的二进制反转输出
位运算基础

  • >>右移动一位
  • <<左移动一位
  • x|=(1<<id)将x的第id位设置成1
  • x>>id&1判断x的第id位是0还是1

对于java而言

  • 右移应该使用无符号右移>>>
  • 左移和无符号左移是同一个符号<<
#define debug
#ifdef debug
#include <time.h>
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
    do { \
       cout << "\033[31;1m " << #x << " -> "; \
       err(x); \
    } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

    char print_f[105];
    void read() {}
    void print() { putchar('\n'); }

    template <typename T, typename... T2>
       inline void read(T &x, T2 &... oth) {
           x = 0;
           char ch = getchar();
           ll f = 1;
           while (!isdigit(ch)) {
               if (ch == '-') f *= -1; 
               ch = getchar();
           }
           while (isdigit(ch)) {
               x = x * 10 + ch - 48;
               ch = getchar();
           }
           x *= f;
           read(oth...);
       }
    template <typename T, typename... T2>
       inline void print(T x, T2... oth) {
           ll p3=-1;
           if(x<0) putchar('-'), x=-x;
           do{
                print_f[++p3] = x%10 + 48;
           } while(x/=10);
           while(p3>=0) putchar(print_f[p3--]);
           putchar(' ');
           print(oth...);
       }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K;
#define set_one(x, id) (x |= (1<<id))

signed main() {
#ifdef debug
    freopen("test.txt", "r", stdin);
    clock_t stime = clock();
#endif
    while(~scanf("%d ", &n)) {
        int p = 0, x;
        while(n--) {
            if(p++) printf(" ");
            scanf("%d ", &x);
            if((x&1)) {
                printf("%d", x);
                continue;
            }
            int max_id = 0, tmp = x;
            while(tmp) { //判断x的最高位1是第几位
                max_id ++;
                tmp >>= 1;
            }
            int ans = 0, id = max_id - 1;
            if(max_id == 1) { //如果只有1位,那必然是1,输出即可
                ans = x;
            } else {
                for(int i=0; i<max_id; i++, id--) { //逆着扫一遍对所有二进制位为1的位置设置为1
                    if((x>>i&1)) //判断第i位是否为1
                        ans |= (1<<id); //设置id位为1
                }
            }
            printf("%d", ans);
        }
        //111010000111
        //111010000111
        //111000010111
        printf("\n");
    }





#ifdef debug
   clock_t etime = clock();
   printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
   return 0;
}



java代码也是一样的

// package FileIO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
//        FileInputStream fis = new FileInputStream("C:\\Users\\majiao\\Desktop\\test");
//        System.setIn(fis);
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()) {
            int n = scan.nextInt();
            int x, cnt = 0, p = 0;
            while(n-- > 0) {
                x = scan.nextInt();
                cnt = 0;
                if(p ++ > 0) System.out.print(" ");
                if(1 == (x&1)) {
                    System.out.print(x);
                    continue;
                } else {
                    int tmp = x;
                    while(tmp > 0) {
                        cnt ++;
                        tmp >>>= 1;
                    }
                    if(cnt == 1) {
                        System.out.print(x);
                        continue;
                    }
                    int id = cnt - 1, ans = 0;
                    for(int i=0; i<cnt; i++, id--) {
                        if(1 == (x>>>i&1)) {
                            ans |= (1 << id);
                        }
                    }
                    System.out.print(ans);
                }
            }
            System.out.println();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值