Topcoder SRM Div2 Level2
2013-06-24 21:04
561 查看
题目: http://community.topcoder.com/stat?c=problem_statement&pm=12399&rd=15489
题目大意:运算O是三种位运算&,||和^之一; 求需要至少多少种输入才能判定O是哪种运算?
解题报告:http://apps.topcoder.com/wiki/display/tc/SRM+569
A non-simulation approach that is faster and needs less code simply requires to analyze the binary operations. Let us make
truth tables for the possible operations.
This is interesting, if for example, both x and y were 0, they would not be useful at differentiating between the operations, because they all return 0 in that case.
If we had x=1, y=0 or vice versa, then we can differentiate between x&y and the others. We would not be able to distinguish between XOR and OR though.
If we had both x=1 and y=1, then we would be able to identify the XOR operation, but not the others.
We need both cases, a case in which x and y are different and a case in which they are both 1 to be able to completely distinguish between the three operations. Note that we cannot use a single plate for both inputs, this
means that we need two plates that have a 1 bit in the current bit position and we also need another plate that has a 0 in this position. As long as the condition is true, we can differentiate between the circuits. The approach is then to just count the number
of plates with 1 and 0 in that bit position, if there are at least two 1s and one 0, then it is possible.
从真值表中可以看到, 1和0 可以判定与运算和或运算, 1和1可以判定异或运算。那么判断三种运算必须需要输入1,1,0. 即1的个数大于2, 0 的个数大于1.
Code
题目大意:运算O是三种位运算&,||和^之一; 求需要至少多少种输入才能判定O是哪种运算?
解题报告:http://apps.topcoder.com/wiki/display/tc/SRM+569
A non-simulation approach that is faster and needs less code simply requires to analyze the binary operations. Let us make
truth tables for the possible operations.
x | y | x&y | x|y | x^y |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
This is interesting, if for example, both x and y were 0, they would not be useful at differentiating between the operations, because they all return 0 in that case.
If we had x=1, y=0 or vice versa, then we can differentiate between x&y and the others. We would not be able to distinguish between XOR and OR though.
If we had both x=1 and y=1, then we would be able to identify the XOR operation, but not the others.
We need both cases, a case in which x and y are different and a case in which they are both 1 to be able to completely distinguish between the three operations. Note that we cannot use a single plate for both inputs, this
means that we need two plates that have a 1 bit in the current bit position and we also need another plate that has a 0 in this position. As long as the condition is true, we can differentiate between the circuits. The approach is then to just count the number
of plates with 1 and 0 in that bit position, if there are at least two 1s and one 0, then it is possible.
从真值表中可以看到, 1和0 可以判定与运算和或运算, 1和1可以判定异或运算。那么判断三种运算必须需要输入1,1,0. 即1的个数大于2, 0 的个数大于1.
Code
class TheDeviceDiv2 { public string identify(string[] plates) { return CanIdentify(plates) ? "YES" : "NO"; } private static bool CanIdentify(string[] plates) { if (plates.Length <= 1) { return false; } int m = plates[0].Length; for (int i = 0; i < m; i++) { int one = 0; int zero = 0; foreach (string plate in plates) { if (plate[i] == '1') { one++; } else { zero++; } } if (one < 2 || zero < 1) { return false; } } return true; } }
相关文章推荐
- XMLHttpRequest level2介绍
- PyTorch学习之路(level2)——自定义数据读取
- Learn Vim Progressively——level1&level2
- PAT天梯赛Level2题目题解汇总
- JavaScript——DOM Level0和DOM Level2事件模型
- JarvisOJ-PWN-Level2
- Level2行情和传统行情的区别
- XMLHttpRequest Level2
- XMLHttpRequest Level2实现跨域
- Rococo(level2 )
- XMLHttpRequest level2介绍
- [pythonchallenge]level2
- 【python challenge】level2
- topcoder SRM500 DIV1 level2 500分
- StaticFactoryMethod_Level2
- topcoder SRM495 div1 level2
- Strategy_Level2
- SRM575 div1 level2
- Word Level1 + Level2 套餐来了!
- 北京地铁线路图纯算法附带求极权值(原创) Level2