zoukankan      html  css  js  c++  java
  • 命令

    krydom 有一个神奇的机器。 一开始,可以往机器里输入若干条指令: opt x 其中,opt 是 & | ^ 中的一种,0 ≤ x ≤ 1023 。 对于 0 到 1023 的每一个数 m,机器会输出 m 按照指令依次运算后的结果。 现在,krydom 往里面输入了 n 条指令,但是 zcysky 觉得 krydom naive, 只用 3 条指令就实现了和这 n 条指令一样的功能。由于 krydom 很菜,希望你 们帮帮他,但是只需要输出 5 条以内的指令就行了。

    直接代码献上:

    #include <cstdio>
    using namespace std;
    int n, x, y, opt, f[11];
    char s[5];
    // and or xor
    int work(int now, int opt){
        if (opt == 1) return 0;
        if (opt == 2) return 1;
        if (now <= 1) return now ^ 1;
            else return 5 - now;
    }
    int output(int x){
        int ans = 0;
        for(int i = 10;i>0; --i){
            ans <<= 1;
            if (f[i] == x) ans |= 1;
        }
        return ans;
    }
    int main(){
        //freopen("program.in", "r", stdin);
    //    freopen("program.out", "w", stdout);
        
        for(int i=1;i<=10;++i) f[i] = 2;
        scanf("%d", &n);
        for(int i=1;i<=n;++i){
            scanf("%s%d", s, &x);
            if (s[0] == '&') opt = 1; 
            else if (s[0] == '|') opt = 2; 
            else opt = 3;//即opt为'^' 
            for(int j=1;j<=10;++j){
                y = x & 1;//判断x的奇偶性 
                if (opt == 1 && !y) f[j] = work(f[j], 1);
                  //如果是'&',f[j]就会在x的二进制转换下变化 
                  //如果x的二进制的j位为0,则f[j]就会变成0; 
                if (opt != 1 && y) f[j] = work(f[j], opt);
              //如果是'|',如果x的二进制的j位为1;则f[j]就会变成1
              //如果是'^',如果x的二进制的j位为1;则:
              //1.如果此时的f[j]为0(1),f[j]就会变成1(0); 
              //2.如果此时的f[j]不为0(1),f[j]就会变成5-f[j]; 
                x/=2;
            }
        }
        puts("3");
        printf("& %d
    ", output(0) ^ 1023);
        printf("| %d
    ", output(1));
        printf("^ %d
    ", output(3));
        
        return 0;
    }
  • 相关阅读:
    浏览器和node中的event loop的区别
    path.resolve(dir)与path.join(__dirname,dir)的区别
    如何在typescript项目中使用eslint
    eslint无法检测ts类型错误
    todo
    brew update 卡住
    async await原理
    node的require
    Hive表头导出成csv文件
    算法--决策树
  • 原文地址:https://www.cnblogs.com/qiuheqiuji/p/11190901.html
Copyright © 2011-2022 走看看