CodeForces

来源:互联网 发布:适合程序员看的电影 编辑:程序博客网 时间:2024/06/04 01:31

总共10位,考虑每个运算对每一位的影响

或0:无影响, 或1:一定为 1, 且0:一定为0,且1:无影响。异或0:无影响,异或1:反转

#include <bits//stdc++.h>using namespace std;char c;int n, a, ans[15], temp[15], a1[15], a2[15], a3[15], x, y, z;int main() {    memset(ans, -1, sizeof(ans));    scanf("%d", &n);    for (int k = 1; k <= n; k++) {        getchar(); scanf("%c", &c);        scanf("%d", &a);        int cnt = 10;        memset(temp, 0, sizeof(temp));        while (a) {            temp[cnt--] = a&1;            a >>= 1;        }//        for (int i = 1; i <= 10; i++) cout << temp[i];//        cout << endl;        for (int i = 1; i <= 10; i++) {            if (c == '|') {                if (temp[i] == 0) {                    continue;                }                else {                    ans[i] = 1;                }            }            else if (c == '&') {                if (temp[i] == 0) {                    ans[i] = 0;                }                else {                    continue;                }            }            else {                if (temp[i] == 0) {                    continue;                }                else {                    if (ans[i] == -1) ans[i] = 2;                    else if (ans[i] == 2) ans[i] = -1;                    else if (ans[i] == 0) ans[i] = 1;                    else if (ans[i] == 1) ans[i] = 0;                }            }        }//        for (int i = 1; i <= 10; i++) cout << ans[i];//        cout << endl;    }    for (int i = 1;  i <= 10; i++) {        if (ans[i] == 0) {            a1[i] = 0;            a2[i] = 0;            a3[i] = 0;        }        else if (ans[i] == 1) {            a1[i] = 1;            a2[i] = 1;            a3[i] = 0;        }        else if (ans[i] == -1) {            a1[i] = 0;            a2[i] = 1;            a3[i] = 0;        }        else if (ans[i] == 2) {            a1[i] = 0;            a2[i] = 1;            a3[i] = 1;        }    }    for (int i = 1; i <= 10; i++) {        x = x * 2 + a1[i];        y = y * 2 + a2[i];        z = z * 2 + a3[i];    }    printf("3\n| %d\n& %d\n^ %d\n", x, y, z);}


原创粉丝点击