zoukankan      html  css  js  c++  java
  • Codeforces Round #443 (Div. 2) C: Short Program

    传送门

    题目大意:

    输入给出一串位运算,输出一个步数小于等于5的方案,正确即可,不唯一。

    题目分析:

    英文题的理解真的是各种误差,从头到尾都以为解是唯一的。
    根据位运算的性质可以知道:

    一连串的位运算最终都可以用三个位运算代替(&|^)。

    那么仅需对每一位的情况进行讨论,某一位:

    • 必须变为1 (|1)
    • 必须变为0 (&0)
    • 必须01颠倒(^1)
      最后输出3种位运算即可(输出三种最稳妥)。

    code

    #include<bits/stdc++.h>
    using namespace std;
    int x, y, n;
    
    int main(){
        //freopen("h.in", "r", stdin);
        scanf("%d", &n);
        x = 0, y = (1<<10)-1;
        for(int i = 1; i <= n; i++){
            char opt[5];
            int v;
            scanf("%s %d", opt + 1, &v);
            if(opt[1] == '|'){
                x |= v;
                y |= v;
            }
            else if(opt[1] == '&'){
                x &= v;
                y &= v;
            }
            else if(opt[1] == '^'){
                x ^= v;
                y ^= v;
            }
        }
        int orr, andd, xorr;
        orr = andd = xorr = 0;
        for(int i = 0; i < 10; i++){
            int t = 1 << i;
            if(x & t){
                if(y & t) orr |= t;
                else xorr |= t;
                andd |= t;
            }
            else{
                if(y & t)
                    andd |= t;
            }
        }
        printf("3
    ");
        printf("& %d
    ", andd);
        printf("| %d
    ", orr);
        printf("^ %d
    ", xorr);
        return 0;
    }
    
    
  • 相关阅读:
    android studio导出apk
    Android开发入门经典实例
    L1-Day32
    L1-Day33
    L1-Day30
    Oracle中的null与空字符串''的区别
    Oracle中的job(定时任务)
    Oracle中的加解密函数
    LeetCode33题——搜索旋转排序数组
    Oracle中的DBMS_LOCK包的使用
  • 原文地址:https://www.cnblogs.com/CzYoL/p/7748516.html
Copyright © 2011-2022 走看看