题目来源:http://poj.org/problem?id=1013

题目大意:有12枚硬币,其中有一枚假币。所有钱币的外表都一样,所有真币的重量都一样,假币的重量与真币不同,但我们不知道假币的重量是比真币轻还是重。现在有一个很准确的天平,我们可以用这个天平称3次来找到那枚假币。只要仔细选择三次称的方式,总可以再三次之内找出那枚假币。

输入:第一行一个正整数n表示样例个数。接下来每三行为一个测试样例。每行为一次称的结果。每枚硬币被编号为A--L。称量的结果有三种,分别用“up”、“down”和“even”表示。第一个字符串表示天平左边的硬币,第二个字符串表示右边的硬币。左边和右边的硬笔数总是相等的。第三个字符串的单词表明天平右边的状态。

输出:对于每个测试用例,输出假币的编号和这枚假币是比真币重还是轻。格式依照Sample output.


Sample Input

1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even

Sample Output

K is the counterfeit coin and it is light.

  注意到以下几点:

  1.某次称量天平平衡,说明天平两端都是真币.

  2.某次天气不平衡,说明这次称量没有用到的都是真币.

  3.如果假币比真币重,则假币只可能每次都出现在天平重的一端(轻则相反),所以若某硬币一次出现在重的一端另一次出现在轻了一端则为真币

  给每枚硬币一个编码,表示其状态。-1表示没有出现,1表示是真币,0表示可能是假币,且比真币轻,2表示可能是假币,且比真币重。

  依据上述观察,每称一次更新一次硬币状态。最终只有一枚硬币不为1。具体见代码。

 //////////////////////////////////////////////////////////////////////////
// POJ1013 Counterfeit Dollar
// Memory: 268K Time: 16MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream>
#include <string>
using namespace std; int main() {
int n;
cin >> n;
for (int i = ; i < n; i++) {
string l[], r[], b[];
int result[];
bool light = false;
for (int i = ; i < ; i++) {
result[i] = -;//未出现
}
cin >> l[] >> r[] >> b[] >> l[] >> r[] >> b[] >> l[] >> r[] >> b[]; for(int i = ; i < ; i++) {
if (b[i].compare("even") == ) {
for (int j = ; j < l[i].size(); j++) {
result[l[i][j] - 'A'] = ;
}
for (int j = ; j < r[i].size(); j++) {
result[r[i][j] - 'A'] = ;
}
} else if (b[i].compare("up") == ) {
bool mark[] = {false, false, false, false, false, false,
false, false, false, false, false, false};
for (int j = ; j < l[i].size(); j++) {
if (result[l[i][j] - 'A'] == -) {
result[l[i][j] - 'A'] = ;
} else if (result[l[i][j] - 'A'] == ) {
result[l[i][j] - 'A'] = ;
}
mark[l[i][j] - 'A'] = true;
}
for (int j = ; j < r[i].size(); j++) {
if (result[r[i][j] - 'A'] == -) {
result[r[i][j] - 'A'] = ;
} else if (result[r[i][j] - 'A'] == ) {
result[r[i][j] - 'A'] = ;
}
mark[r[i][j] - 'A'] = true;
}
for (int t = ; t < ; t++) {
if (mark[t] == false) {
result[t] = ;
}
}
} else {
bool mark[] = {false, false, false, false, false, false,
false, false, false, false, false, false};
for (int j = ; j < l[i].size(); j++) {
if (result[l[i][j] - 'A'] == -) {
result[l[i][j] - 'A'] = ;
} else if (result[l[i][j] - 'A'] == ) {
result[l[i][j] - 'A'] = ;
}
mark[l[i][j] - 'A'] = true;
}
for (int j = ; j < r[i].size(); j++) {
if (result[r[i][j] - 'A'] == -) {
result[r[i][j] - 'A'] = ;
} else if (result[r[i][j] - 'A'] == ) {
result[r[i][j] - 'A'] = ;
}
mark[r[i][j] - 'A'] = true;
}
for (int t = ; t < ; t++) {
if (mark[t] == false) {
result[t] = ;
}
}
}
}
for (int i = ; i < ; i++) {
if (result[i] == ) {
cout << char(i + 'A') << " is the counterfeit coin and it is light." << endl;
break;
} else if (result[i] == ) {
cout << char(i + 'A') << " is the counterfeit coin and it is heavy." << endl;
break;
}
}
}
system("pause");
}

附Discuss里面的一些测试数据:

 sample input 

 ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
AGHL BDEC even
JKI ADE up
J K even
ABCDEF GHIJKL up
ABC DEF even
I J down
ABCDEF GHIJKL up
ABHLEF GDIJKC down
CD HA even
A B up
B A down
A C even
A B up
B C even
DEFG HIJL even
ABC DEJ down
ACH IEF down
AHK IDJ down
ABCD EFGH even
AB IJ even
A L down
EFA BGH down
EFC GHD even
BA EF down
A B up
A C up
L K even
ACEGIK BDFHJL up
ACEGIL BDFHJK down
ACEGLK BDFHJI down
ACEGIK BDFHJL up
ACEGIL BDFHJK down
ACEGLK BDFHJI up sample output
K is the counterfeit coin and it is light.
I is the counterfeit coin and it is heavy.
I is the counterfeit coin and it is light.
L is the counterfeit coin and it is light.
B is the counterfeit coin and it is light.
A is the counterfeit coin and it is heavy.
A is the counterfeit coin and it is light.
L is the counterfeit coin and it is heavy.
A is the counterfeit coin and it is light.
A is the counterfeit coin and it is heavy.
L is the counterfeit coin and it is light.
K is the counterfeit coin and it is heavy.

Test data & answer

POJ1013 Counterfeit Dollar的更多相关文章

  1. poj1013.Counterfeit Dollar(枚举)

    Counterfeit Dollar Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 415  Solved: 237 Description Sally ...

  2. Counterfeit Dollar -----判断12枚钱币中的一个假币

     Counterfeit Dollar Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  3. POJ 1013 Counterfeit Dollar

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36206   Accepted: 11 ...

  4. Counterfeit Dollar 分类: POJ 2015-06-12 15:28 19人阅读 评论(0) 收藏

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41559   Accepted: 13 ...

  5. Poj 1013 Counterfeit Dollar / OpenJudge 1013(2692) 假币问题

    1.链接地址: http://poj.org/problem?id=1013 http://bailian.openjudge.cn/practice/2692 http://bailian.open ...

  6. POJ 1013:Counterfeit Dollar

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42028   Accepted: 13 ...

  7. 【poj1013】 Counterfeit Dollar

    http://poj.org/problem?id=1013 (题目链接) 题意 12个硬币中有1个是假的,给出3次称重结果,判断哪个硬币是假币,并且判断假币是比真币中还是比真币轻. Solution ...

  8. POJ 1013 Counterfeit Dollar 集合上的位运算

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

  9. D - Counterfeit Dollar(第二季水)

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

随机推荐

  1. Angular.forEach用法

    1.针对对象循环(key,value) 官方示例: var values = {name: 'misko', gender: 'male'}; var log = []; angular.forEac ...

  2. Operating System-Thread(2) Multi-Process-Parallel vs Multi-Thread-Parallel

    本文主要介绍线程的模型 一.Multi-Process-Parallel vs Multi-Thread-Parallel 多进程的并行:CPU在多个进程之间进行切换,系统给人一种多个进程在并行执行的 ...

  3. Python 中list, dictionary 与 file相互操作

    Python的list,dictionary可以写入file, 也可以从file中读取. 关于list: 1)写入文件         self.existedBlog.write("you ...

  4. JavaScript-Tool-富文本:UEditor

    ylbtech-JavaScript-Tool-富文本:UEditor UEditor是由百度WEB前端研发部开发的所见即所得的开源富文本编辑器,具有轻量.可定制.用户体验优秀等特点.开源基于BSD协 ...

  5. jquery 中post 、get的同步问题,从外部获取返回数据

    解决方法1: 在全局设置: $.ajaxSetup({ async : false }); $.ajaxSetup({ async : false }); 然后再使用post或get方法 $.get( ...

  6. python unittest之断言及示例

    python unintest单元测试框架提供了一整套内置的断言方法. 如果断言失败,则抛出一个AssertionError,并标识该测试为失败状态 如果异常,则当做错误来处理 注意:以上两种方式的区 ...

  7. LoadRunner 12 模拟 RSA加密 登录的实现(JS)

    LR 12 中 web_js_run API 非常坑,只能调用一个 JS 文件:更坑的是,不能通用 一个JS调用另外一个JS:(可能有,但在网上找了N个国家,都没有找到!如有,还请朋友告之,谢谢.) ...

  8. assert.equal()

    assert.equal(actual, expected[, message]) 使用相等运算符(==)测试 actual 参数与 expected 参数是否相等(通俗解释equal方法接受三个参数 ...

  9. [原创]SQL表值函数:返回从当前周开始往回的自定义周数

    一如往常一样,一篇简短博文记录开发过程中遇到的一个问题.初衷都是记录自己的一些Idea,也是希望能够帮助一些凑巧遇到此类需求的问题,这个需求的的开端是因为,要统计最近N周的销售数据. 接下来我们来看看 ...

  10. python drift

    install dependency(optional):(本文来自 不才b_d 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/sinat_36184075/arti ...