在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的更多相关文章

  1. POJ1013 Counterfeit Dollar

    题目来源:http://poj.org/problem?id=1013 题目大意:有12枚硬币,其中有一枚假币.所有钱币的外表都一样,所有真币的重量都一样,假币的重量与真币不同,但我们不知道假币的重量 ...

  2. poj1013.Counterfeit Dollar(枚举)

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

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

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

  4. POJ 1013 Counterfeit Dollar

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

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

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

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

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

  7. POJ 1013:Counterfeit Dollar

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

  8. 【poj1013】 Counterfeit Dollar

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

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

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

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

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

随机推荐

  1. Java数列循环左移

    描述 有n个整数组成一个数组(数列).现使数列中各数顺序依次向左移动k个位置,移出的数再从尾部移入.输出移动后的数列元素. 题目没有告诉你n的范围,要求不要提前定义数组的大小. 另外要求定义并使用函数 ...

  2. scrapy框架综合运用 爬取天气预报 + 定时任务

    爬取目标网站: http://www.weather.com.cn/ 具体区域天气地址: http://www.weather.com.cn/weather1d/101280601.shtm(深圳) ...

  3. jsp环境搭建

    jsp开发环境需要JDK与Tomcat,需先下载JDK组件与tomcat组件 JDK地址:https://www.oracle.com/technetwork/java/javase/download ...

  4. Hdu1042 N! (阶乘高精度模板)

    Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N in ...

  5. Linux网络课程学习第一天

    第一天上课主要介绍了LINUX系统和Linux课程的情况.了解了开源系统的四大优势,六大特点. 最具有心得的一句话: 学习是件苦差事: 稻盛和夫先生的话也深深激励着我:“工作马马虎虎,只想在兴趣和游戏 ...

  6. javaScript中的异步编程模式

    1.事件模型 let button = document.getElementById("my-btn"); button.onclick = function(event) { ...

  7. Selenium3+python自动化014-自动化测试框架的作用

    1.能够有效组织和管理测试脚本 2.进行数据驱动或者关键字驱动的测试 3.将基础的测试代码进行封装,降低测试脚本编写的复杂性和重复性 4.提高测试脚本维护和修改的效率 5.自动执行测试脚本,并自动发布 ...

  8. CSS之 元素显示隐藏,用户界面样式,文本溢出隐藏,精灵技术,三角形

    元素的显示与隐藏 display 显示 display 设置或检索对象是否及如何显示 display: none; 隐藏对象 display: block; 除了转换为块级元素, 同时还有显示元素的意 ...

  9. C#常规TcpListener

    1.Xaml <Window x:Class="Server.MainWindow" xmlns="http://schemas.microsoft.com/win ...

  10. react-绑定this并传参的三种方式

    绑定this并传参的三种方式 在事件中绑定this并传参: <input type="button" value="在事件中绑定this并传参" onCl ...