zoukankan      html  css  js  c++  java
  • 【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;
    }
    
  • 相关阅读:
    函数二
    python控制台输出带颜色的文字方法
    is 和 == 的区别
    基本数据类型(dict)
    基本数据类型(list,tuple)
    基本数据类型(int,bool,str)
    Python运算符与编码
    Java并发编程:synchronized
    泛型中? super T和? extends T的区别
    java中的匿名内部类总结
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7741582.html
Copyright © 2011-2022 走看看