[不会]位运算不懂例子

 1 #include<iostream>
 2 using namespace std;
 3 int main()  {
 4     int a,b;
 5     int n;
 6 while(cin >> n)    {
 7         cin >> a;
 8         for(int i=1;i<n;i++)  {
 9             cin >> b;
10             a^=b;
11         }
12         cout << a << endl;
13     }
14     return 0;
15 }

二进制的遍历:

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int a[5] = {1,3,5,6,8};
 6     int n;
 7     n = 5;
 8     for(int i = 0;i < (1 << n); i++) {//从0~2^n-1个状态,遍历每一个集合
 9         for(int j = 0; j < n; j++) { //遍历二进制的每一位
10             if(i & (1 << j)){   //判断二进制第j位是否存在
11                 cout << a[j] << ' ';
12             }
13         }
14         cout << endl;
15     }
16     return 0;
17  } 

猜你喜欢

转载自www.cnblogs.com/waryan/p/12200598.html