非常遗憾,没有弄懂题主“按位或”一题的思路。我将代码调成如下形式以便于查看内部的运算步骤 #include<cstdio> #include<iostream> #include<bitset> using namespace std; int p[10] = {0}; int mx = 10 ; int q ; int main() { scanf("%d",&q) ; while(q--) { int op , x;scanf("%d%d",&op,&x) ; if(op == 1) { bitset<4> b_x(x); cout << "x = " << b_x << endl; if(p[x] == x){ printf("p[%d] == %d", x, x); continue; } int s = mx ^ x; bitset<4> b_s(s); bitset<4> b_mx(mx); cout << "mx ^ x = "<< b_mx << " ^ " << b_x << " = " << b_s << endl; for(int i = s ; i ; i = (i - 1) & s) { bitset<4> b_i(i); bitset<4> b_pbefore(p[i ^ x]); bitset<4> b_ix(i ^ x); p[i ^ x] |= x ; bitset<4> b_pafter(p[i ^ x]); cout << "p[i ^ x] = p[i ^ x] | x ="<< "p[ "<< b_ix<<" ] | "<< b_x <<" = " << "p[ "<< (i ^ x) <<" ] | "<< x <<" = "<< b_pbefore << " | " << b_x << " = " << b_pafter<< endl; } p[x] = x; } else { if(p[x] == x) puts("YES") ; else puts("NO"); } } return 0; } 然后得到了如下输出: 10 1 4 x = 0100 mx ^ x = 1010 ^ 0100 = 1110 p[i ^ x] = p[i ^ x] | x =p[ 1010 ] | 0100 = p[ 10 ] | 4 = 1001 | 0100 = 1101 p[i ^ x] = p[i ^ x] | x =p[ 1000 ] | 0100 = p[ 8 ] | 4 = 0000 | 0100 = 0100 p[i ^ x] = p[i ^ x] | x =p[ 1110 ] | 0100 = p[ 14 ] | 4 = 0000 | 0100 = 0100 p[i ^ x] = p[i ^ x] | x =p[ 1100 ] | 0100 = p[ 12 ] | 4 = 0000 | 0100 = 0100 p[i ^ x] = p[i ^ x] | x =p[ 0010 ] | 0100 = p[ 2 ] | 4 = 0000 | 0100 = 0100 p[i ^ x] = p[i ^ x] | x =p[ 0000 ] | 0100 = p[ 0 ] | 4 = 0000 | 0100 = 0100 p[i ^ x] = p[i ^ x] | x =p[ 0110 ] | 0100 = p[ 6 ] | 4 = 0000 | 0100 = 0100 2 5 NO 1 9 x = 1001 mx ^ x = 1010 ^ 1001 = 0011 p[i ^ x] = p[i ^ x] | x =p[ 1010 ] | 1001 = p[ 10 ] | 9 = 1011 | 1001 = 1011 p[i ^ x] = p[i ^ x] | x =p[ 1011 ] | 1001 = p[ 11 ] | 9 = 0000 | 1001 = 1001 p[i ^ x] = p[i ^ x] | x =p[ 1000 ] | 1001 = p[ 8 ] | 9 = 0100 | 1001 = 1101 1 15 x = 1111 mx ^ x = 1010 ^ 1111 = 0101 p[i ^ x] = p[i ^ x] | x =p[ 1010 ] | 1111 = p[ 10 ] | 15 = 1010 | 1111 = 1111 p[i ^ x] = p[i ^ x] | x =p[ 1011 ] | 1111 = p[ 11 ] | 15 = 1001 | 1111 = 1111 p[i ^ x] = p[i ^ x] | x =p[ 1110 ] | 1111 = p[ 14 ] | 15 = 0100 | 1111 = 1111 2 4 YES 1 11 x = 1011 mx ^ x = 1010 ^ 1011 = 0001 p[i ^ x] = p[i ^ x] | x =p[ 1010 ] | 1011 = p[ 10 ] | 11 = 1101 | 1011 = 1111 2 10 NO 2 7 NO 2 9 YES 2 4 YES 我不清楚int s = mx ^ x;一句的含义。不清楚如下循环的含义 for(int i = s ; i ; i = (i - 1) & s) { p[i ^ x] |= x ; } 以及上述循环为什么在4输入时,将p[10, 8, 14, 12, 2, 0, 6]依次赋值,后者的值是前者对x的异或。
点赞 1

相关推荐

把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
牛客网
牛客企业服务