【Codeforces Round #443 (Div. 2) C】Short Program

【链接】 我是链接,点我呀:)
【题意】

给你一个n行的只和位运算有关的程序。 让你写一个不超过5行的等价程序。 使得对于每个输入,它们的输出都是一样的。

【题解】

先假设x=1023,y=0; 即每位都是1和每位都是0; 然后做一下这n个操作。 得出,每一位如果是0的话输出应该是几,以及每一位是1的话输出应该是几. 会发现,用or和xor和and就能完成.

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int n,a,b;

int main(){
#ifdef LOCAL_DEFINE
    freopen("rush.txt", "rt", stdin);
#endif

	scanf("%d",&n);
	char s[5];int d1;
	a = 1023,b = 0;
	for (int i = 1;i <= n;i++){
		scanf("%s%d",s,&d1);
		if (s[0]=='|') {
			a|=d1,b|=d1;
		}
		if (s[0]=='^') a^=d1,b^=d1;
		if (s[0]=='&') a&=d1,b&=d1;
	}
	int numor = 0,numand = 0,numxor = 0;
	for (int i = 0;i < 10;i++){
		int x = (1<<i);
		if ((a&x) && (b&x)){
			numor += x;			
		}
		if (!(a&x) && (b&x)){
			numxor+=x;
		}
		if (!(a&x) && !(b&x)){
			numor += x,numxor += x;
		}
	}

	int ans = (numor>0) + (numand>0)+(numxor>0);
	printf("%d
",ans);	
	if (numor){
		printf("| %d
",numor);
	}
	if (numand){
		printf("& %d
",numand);
	}
	if (numxor){
		printf("^ %d
",numxor);
	}

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.
";
#endif
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7741582.html