Java-POJ1013-Counterfeit Dollar
在13枚硬币中找出fake的那一个
输入:三次天平称量结果
package poj.ProblemSet; import java.util.Scanner; /*
我怎么觉得是贪心算法呢?
起初对所有硬币标记0;
如果是even,则两边所有的硬币记为真(记233);
否则就对不确定的硬币记录怀疑(++或者--根据天平倾斜方向);
最后只要看哪个硬币的绝对值最大,也就是被怀疑的次数最多,即是假币。
*/
public class poj1013 {
public static int[] value = new int[20];
public static String[] a = new String[5];
public static String[] b = new String[5];
public static String[] c = new String[5];
public static final int REAL = 233; public static void work(int r, int len, int type) {
if (type == 1) {
for (int i = 0; i < len; i++) {
if (value[a[r].charAt(i)-'A'+1] != REAL) value[a[r].charAt(i)-'A'+1]++;
if (value[b[r].charAt(i)-'A'+1] != REAL) value[b[r].charAt(i)-'A'+1]--;
}
}
else if (type == 2) {
for (int i = 0; i < len; i++) {
if (value[a[r].charAt(i)-'A'+1] != REAL) value[a[r].charAt(i)-'A'+1]--;
if (value[b[r].charAt(i)-'A'+1] != REAL) value[b[r].charAt(i)-'A'+1]++;
}
}
else /*if (type == 3)*/ {
for (int i = 0; i < len; i++) {
value[a[r].charAt(i)-'A'+1] = REAL;
value[b[r].charAt(i)-'A'+1] = REAL;
}
} } public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
for (int n = cin.nextInt(); n-- > 0; ) {
for (int i = 1; i < 20; i++) value[i] = 0;
for (int r = 1; r <= 3; r++) {
a[r] = cin.next();b[r] = cin.next();c[r] = cin.next();int len = a[r].length();
if (c[r].equals("up")) work(r, len, 1);
else if (c[r].equals("down")) work(r, len, 2);
else /*if (c[r].equals("even"))*/ work(r, len, 3);
}
int max = 0,id = 0;
for (int i = 1; i <= 13; i++) {
int val = Math.abs(value[i]);
if (value[i] != REAL && val > max) { max = val;id = i; }
}
System.out.println((char)(id-1+'A') + " is the counterfeit coin and it is " + (value[id]>0?"heavy":"light") + ".");
}
}
}
Java-POJ1013-Counterfeit Dollar的更多相关文章
- POJ1013 Counterfeit Dollar
题目来源:http://poj.org/problem?id=1013 题目大意:有12枚硬币,其中有一枚假币.所有钱币的外表都一样,所有真币的重量都一样,假币的重量与真币不同,但我们不知道假币的重量 ...
- poj1013.Counterfeit Dollar(枚举)
Counterfeit Dollar Time Limit: 1 Sec Memory Limit: 64 MB Submit: 415 Solved: 237 Description Sally ...
- Counterfeit Dollar -----判断12枚钱币中的一个假币
Counterfeit Dollar Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- POJ 1013 Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36206 Accepted: 11 ...
- Counterfeit Dollar 分类: POJ 2015-06-12 15:28 19人阅读 评论(0) 收藏
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41559 Accepted: 13 ...
- Poj 1013 Counterfeit Dollar / OpenJudge 1013(2692) 假币问题
1.链接地址: http://poj.org/problem?id=1013 http://bailian.openjudge.cn/practice/2692 http://bailian.open ...
- POJ 1013:Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42028 Accepted: 13 ...
- 【poj1013】 Counterfeit Dollar
http://poj.org/problem?id=1013 (题目链接) 题意 12个硬币中有1个是假的,给出3次称重结果,判断哪个硬币是假币,并且判断假币是比真币中还是比真币轻. Solution ...
- POJ 1013 Counterfeit Dollar 集合上的位运算
Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...
- D - Counterfeit Dollar(第二季水)
Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...
随机推荐
- iframe在iphone手机上的问题
问题1: 通过document.addEventListener("scroll",function(){})对页面滚动监听事件进行监听,但ios下$(document).scro ...
- Easyui-Treegrid使用注意事项-sunziren
版权声明:本文为sunziren原创文章,博客园首发,转载务必注明出处以及作者名称. 最近,工作中有一个网页需要用到前端框架easyui的treegrid组件,因此我对这个treegird研究了一段时 ...
- LOJ #6402. yww 与校门外的树 多项式求逆
蛮神的一道题. code: #include <cmath> #include <cstring> #include <algorithm> #include &l ...
- 【3】Python中的广播
Python-numpy中有一种很高效的方法:广播. 下面介绍一下广播. 实例:对于这个矩阵,如果想求每列元素的和,怎么才能不用for循环? (1,4)指的是一行四列的矩阵:axis决定了是横向(行 ...
- ubuntu set up 6 - NTFS Mount
1. NTFS Mounted as read-only https://askubuntu.com/questions/1138076/ubuntu-18-04-cant-write-on-ntfs ...
- BZOJ3926&&lg3346 ZJOI诸神眷顾的幻想乡(广义后缀自动机)
BZOJ3926&&lg3346 ZJOI诸神眷顾的幻想乡(广义后缀自动机) 题面 自己找去 HINT 我们可以把题目拆解成几个部分,首先我们手玩一个结论,从所有的叶子节点出发,遍历整 ...
- idea 2018.1.2激活方法,有效期至2099年
1. 下载破解补丁文件JetbrainsCrack-2.7-release-str.jar 链接: https://pan.baidu.com/s/1inWaS067RPte3ZkD6uDxOQ 密码 ...
- pandas玩转excel-> (2)如何利用pandas读取excel数据文件
import pandas as pd #将excel文件读到内存中,形成dataframe,并命名为peoplepeople=pd.read_excel('D:/python结果/task2/Peo ...
- Codeforces Round #616 (Div. 2) C. Mind Control
题目链接:http://codeforces.com/contest/1291/problem/C 思路: 我们可以很容易想到,只有前m-1个人才能影响m的选择的大小,后面的人无法影响. 如果所有人都 ...
- 前端面试必备技巧(二)css盒模型及BFC
CSS盒模型 基本概念:标准模型+IE模型及区别 CSS如何设置这两种模型? JS如何设置获取盒模型对应的宽和高? 实例题(根据盒模型解释边距重叠) BFC边距重叠解决方案 (1)BFC的基本概念:b ...