## Codeforces 879C/878A - Short Program

“&”: 0&0=0，0&1=0，1&0=0，1&1=1

“|”: 0|0=0，0|1=1，1|0=1，1|1=1

“^”: 0^0=0，0^1=1，1^0=1，1^1=0

f[i]:0->0,1->0，则f[i]=“&0,|0,^0”，即a[i]=0,b[i]=0,c[i]=0

f[i]:0->0,1->1，则f[i]=“&1,|0,^0”，即a[i]=1,b[i]=0,c[i]=0

若f[i]:0->1,1->0，则f[i]=“&1,|0,^1”，即a[i]=1,b[i]=0,c[i]=1

f[i]:0->1,1->1，则f[i]=“&1,|1,^0”，即a[i]=1,b[i]=1,c[i]=0

#include <stdio.h>

int main(void)
{
int n;
int x = 0, y = 0x3ff;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
getchar();
char op = getchar();
int num;
scanf("%d", &num);
switch (op) {
case '&':
x &= num;
y &= num;
break;
case '|':
x |= num;
y |= num;
break;
case '^':
x ^= num;
y ^= num;
break;
default:
break;
}
}
int num_and = 0, num_or = 0, num_xor = 0;
for (int i = 0; i < 10; i++) {
int bit = 1 << i;
if ((x & bit)) {
if (y & bit) num_or |= bit;
else num_xor |= bit;
num_and |= bit;
} else {
if (y & bit) num_and |= bit;
}
}
printf("3\n& %d\n| %d\n^ %d\n", num_and, num_or, num_xor);
return 0;
}

f[i]:0->0,1->0，则f[i]=“&0,|0,^0”，即a[i]=0,b[i]=0,c[i]=0

f[i]:0->0,1->1，则f[i]=“&1,|0,^0”，即a[i]=1,b[i]=0,c[i]=0

若f[i]:0->1,1->0，则f[i]=“&1,|0,^1”，即a[i]=1,b[i]=0,c[i]=1

f[i]:0->1,1->1，则f[i]=“&1,|1,^0”，即a[i]=1,b[i]=1,c[i]=0

a=f(0x0)|f(0x3ff)

b=f(0x0)&f(0x3ff)

c=f(0x0)&(0x3ff^f(0x3ff))

#include <stdio.h>

int main(void)
{
int n;
int x = 0, y = 0x3ff;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
getchar();
char op = getchar();
int num;
scanf("%d", &num);
switch (op) {
case '&':
x &= num;
y &= num;
break;
case '|':
x |= num;
y |= num;
break;
case '^':
x ^= num;
y ^= num;
break;
default:
break;
}
}
int num_and = x | y,
num_or = x & y,
num_xor = x & (0x3ff ^ y);
printf("3\n& %d\n| %d\n^ %d\n", num_and, num_or, num_xor);
return 0;
}

posted on 2017-10-27 15:24  SiuGinHung  阅读(1107)  评论(0编辑  收藏  举报