2021.01.23 Rating补题

C题

题目大意:

给出n个操作,每次操作进行一次 | ^ &,让你简化步骤使得最终的5步内能够得到一样的答案。

思路:

这个看到首先想化简后的操作,会不会能够仅让 | ^ &每进行一次得到一样的答案,即x&a|b^c的形式,从而转向算a,b,c的方向求解,又因位操作时对单个位进行的,所以我们可以先让放入几个特殊值,来进行运算,比如000000 与 111111,这样的数进行位操作时,可以清晰得到那些位发生了变化,通过这两数的变化,进而推得a,b,c的值。

解题代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
using namespace std;
const long long N = 1e10 + 7;
const int maxn = 2e5 + 5;
const long long INF = 8e18;
typedef long long ll;
#define for0(i,n) for(int i = 0;i < n;i++)
#define for1(i,n) for(int i = 1;i <= n;i++)
int main()
{
    int a,b;
    a = 0,b = 1023;
    int n;
    cin >> n;
    char s[100];
    int num;
    while(n--)
    {
        cin >> s >> num;

        if(s[0] == '|')
        {
            a |= num;
            b |= num;
        }
        else if(s[0] == '&')
        {
            a &= num;
            b &= num;
        }
        else if(s[0] == '^')
        {
            a ^= num;
            b ^= num;
        }
    }
    int tand = 0,tor = 0,txor = 0;
    tand = a | b;
    tor = a & b;
    txor = a & (b ^ 1023);
    cout << 3 << endl;
    printf("| %d\n",tor);
    printf("& %d\n",tand);
    printf("^ %d\n",txor);

    return 0;
}

 

posted @ 2021-01-27 14:43  emhhbw==  阅读(171)  评论(0)    收藏  举报