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 ...
随机推荐
- C# interact with Command prompt
using System.IO; using System.Diagnostics; static void Main(string[] args) { CmdDemo("dir" ...
- MacOs使用CleanMyMac X清除可清除空间
写在前面 本文介绍如何使用CleanMyMac X清除可清除的空间 可以看到,可清除的空间达到了125.79GB,虽然说不影响系统的使用,但是在使用时间机器进行备份的时候,仍然会将可清除空间当成备份的 ...
- flex 属性
一.flex-direction(元素排列方向) 1.flex-direction:row://从左到右排列 2.flex-direction:column://从上往下排列 二.flex-wrap( ...
- Npoi常用操作方法介绍
1.ShiftRows(startRow,endRow,moveRows) 将开始行到结束行向上或者向下移动moveRows行,moveRows为正数向下移动,为负数向上移动(向上移动,会把之前的行覆 ...
- linux100讲——80 系统函数库介绍
1.系统自建了函数库,可以在脚本中引用 /etc/init.d/functions 2.自建函数库 使用 source 函数脚本文件 “导入”函数 3. vim /etc/init.d/functio ...
- arm的字节对齐问题总结(转)
问题由来:pc的lsb总是0,因为代码至少要字对齐.cm3的指令至少是半字对齐的(16) 一.啥是字对齐?为啥要字对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访 ...
- H5_0009:关于HTML5中Canvas的宽、高设置问题
关于HTML5中Canvas的宽.高设置问题 Canvas元素默认宽 300px, 高 150px, 设置其宽高可以使用如下方法(不会被拉伸): 方法一: <canvas widt ...
- Ignatius and the Princess IV HDU - 1029 基础dp
#include<iostream> #include<cstring> #include<cmath> #include<cstdio> #inclu ...
- 18新生赛 4. Deal
题目描述:双十一过后,syx发现自己快要吃土了.但是机智的他决定理财.他预测了将来n天的比特币行情,发现有涨有跌,有跌有涨.手里的钱只要在比特币的浪潮中经历沉浮,低价收入,高价卖出,就可以轻易割到别人 ...
- python 中 if __name__ == '__main__' 判断的作用
假设这样一个a.py文件 def fun1(): ........ def fun2(): ......... if __name__=='__main__': ......#执行的一些语句 当你执行 ...