CodeForces - 878A Short Program——构造

网友投稿 548 2022-11-07

CodeForces - 878A Short Program——构造

CodeForces - 878A Short Program——构造

总共10位,考虑每个运算对每一位的影响

或0:无影响, 或1:一定为 1, 且0:一定为0,且1:无影响。异或0:无影响,异或1:反转

#include using namespace std;char c;int n, a, ans[15], temp[15], a1[15], a2[15], a3[15], x, y, z;int main() { memset(ans, -1, sizeof(ans)); scanf("%d", &n); for (int k = 1; k <= n; k++) { getchar(); scanf("%c", &c); scanf("%d", &a); int cnt = 10; memset(temp, 0, sizeof(temp)); while (a) { temp[cnt--] = a&1; a >>= 1; }// for (int i = 1; i <= 10; i++) cout << temp[i];// cout << endl; for (int i = 1; i <= 10; i++) { if (c == '|') { if (temp[i] == 0) { continue; } else { ans[i] = 1; } } else if (c == '&') { if (temp[i] == 0) { ans[i] = 0; } else { continue; } } else { if (temp[i] == 0) { continue; } else { if (ans[i] == -1) ans[i] = 2; else if (ans[i] == 2) ans[i] = -1; else if (ans[i] == 0) ans[i] = 1; else if (ans[i] == 1) ans[i] = 0; } } }// for (int i = 1; i <= 10; i++) cout << ans[i];// cout << endl; } for (int i = 1; i <= 10; i++) { if (ans[i] == 0) { a1[i] = 0; a2[i] = 0; a3[i] = 0; } else if (ans[i] == 1) { a1[i] = 1; a2[i] = 1; a3[i] = 0; } else if (ans[i] == -1) { a1[i] = 0; a2[i] = 1; a3[i] = 0; } else if (ans[i] == 2) { a1[i] = 0; a2[i] = 1; a3[i] = 1; } } for (int i = 1; i <= 10; i++) { x = x * 2 + a1[i]; y = y * 2 + a2[i]; z = z * 2 + a3[i]; } printf("3\n| %d\n& %d\n^ %d\n", x, y, z);}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:UVa 10881 Piotr's Ants ——思路题
下一篇:UVa 1149 Bin Packing——优先队列
相关文章

 发表评论

暂时没有评论,来抢沙发吧~