在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. Android数据存储之SD卡文件操作

    赶上智能手机刚问世时有幸用过的小伙伴都知道,那时候的Android系统是把我们自己买的SD卡作为外部存储的,但是不知道从哪天开始,SD卡就被固化到智能手机的内部了,但是我们仍然把它称为外部存储.我想A ...

  2. Java(四)输出和输入函数

    介绍一下Java里简单常用的输入输出方法. Java的输出函数很简单,直接调用System类的out对象的print函数即可. 代码: System.out.print(a);//输出变量a的值 Sy ...

  3. ASP.NET Identity系列教程-4【Identity高级技术】

    https://www.cnblogs.com/r01cn/p/5194257.html 15 ASP.NET Identity高级技术 In this chapter, I finish my de ...

  4. ubuntu set up 6 - NTFS Mount

    1. NTFS Mounted as read-only https://askubuntu.com/questions/1138076/ubuntu-18-04-cant-write-on-ntfs ...

  5. SpringBoot2.x打包成war(看这篇就够了)

    springboot默认打包成jar,如果想打包成war,则需要做以下三步. 1.修改pom.xml文件 a.将jar改成war <groupId>com.test</groupId ...

  6. 【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator)

    [学习资料] <C#图解教程>(第18章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...

  7. P5331 [SNOI2019]通信 [线段树优化建图+最小费用最大流]

    这题真让人自闭-我EK费用流已经死了?- (去掉define int long long就过了) 我建的边害死我的 spfa 还是spfa已经死了? 按费用流的套路来 首先呢 把点 \(i\) 拆成两 ...

  8. 消息队列和Kafka

    ------20191211闪

  9. MPI Maelstrom POJ - 1502 floyd

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> usin ...

  10. LaTeX技巧012:LaTeX 插图加载宏包

    LaTeX 插图加载宏包.支持 LaTeX - DVIPDFMx; pdfLaTeX; XeLaTeX 三种编译方式,支持 eps/pdf/jpg/png 等图片格式. % Put this snip ...