九度OJ 1150:Counterfeit Dollar(假美元) (分析、检验)
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:485
解决:215
- 题目描述:
-
Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different
weight from the other coins but Sally does not know if it is heavier or lighter than the real coins.
Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two
coins are true. Now if Sally weighs one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed
goes up or down, respectively.
By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.
- 输入:
-
For each case, the first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A--L. Information on a weighing
will be given by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same
number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.
- 输出:
-
For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.
- 样例输入:
-
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
- 样例输出:
-
K is the counterfeit coin and it is light.
思路:
简单的说,有12枚硬币,1枚是假的,重量与其他的不一样,但不知道是重还是轻。有一个天平可以称,只要三次就能找出来。
现在给出三次称的结果,求那一枚是假的,重还是轻。
有两种方法,一种是分析,需要较强的逻辑;还有一种方法我更推荐,直接24种情况代入检验,满足结果即是答案。
我在九度上的AC代码在POJ上未通过,原因是不一定天平两边都是四枚硬币。
代码1是九度上的C代码,代码2是在POJ上的C++代码。
代码1:
#include <stdio.h>
#include <string.h> int c2i(char c)
{
return c-'A';
} char i2c(int i)
{
return i+'A';
} int trans(char s[])
{
if (strcmp(s, "even") == 0)
return 0;
else if (strcmp(s, "up") == 0)
return 1;
else
return -1;
} int main(void)
{
int i, j, k, r;
int n, res[3];
char s1[3][5], s2[3][5], s3[3][5]; while (scanf("%d", &n) != EOF)
{
while (n--)
{
for (i=0; i<3; i++)
{
scanf("%s%s%s", s1[i], s2[i], s3[i]);
res[i] = trans(s3[i]);
}
int flag = 0;;
for (j=-1; j<=1; j+=2)
{
for (k=0; k<12; k++)
{
for (i=0; i<3; i++)
{
int value = 0;
for (r=0; r<4; r++)
{
if (c2i(s1[i][r]) == k)
value = j;
if (c2i(s2[i][r]) == k)
value = -j;
}
if (value != res[i])
break;
}
if (i == 3)
{
flag = 1;
break;
}
}
if (flag == 1)
break;
}
if (j == -1)
printf("%c is the counterfeit coin and it is light.\n", i2c(k));
else
printf("%c is the counterfeit coin and it is heavy.\n", i2c(k));
}
} return 0;
}
/**************************************************************
Problem: 1150
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/
代码2:
#include <stdio.h>
#include <string.h> int c2i(char c)
{
return c-'A';
} char i2c(int i)
{
return i+'A';
} int trans(char s[])
{
if (strcmp(s, "even") == 0)
return 0;
else if (strcmp(s, "up") == 0)
return 1;
else
return -1;
} int main(void)
{
int i, j, k, r;
int n, res[3];
char s1[3][5], s2[3][5], s3[3][5]; while (scanf("%d", &n) != EOF)
{
while (n--)
{
for (i=0; i<3; i++)
{
scanf("%s%s%s", s1[i], s2[i], s3[i]);
res[i] = trans(s3[i]);
}
int flag = 0;;
for (j=-1; j<=1; j+=2)
{
for (k=0; k<12; k++)
{
for (i=0; i<3; i++)
{
int value = 0;
for (r=0; r<4; r++)
{
if (c2i(s1[i][r]) == k)
value = j;
if (c2i(s2[i][r]) == k)
value = -j;
}
if (value != res[i])
break;
}
if (i == 3)
{
flag = 1;
break;
}
}
if (flag == 1)
break;
}
if (j == -1)
printf("%c is the counterfeit coin and it is light.\n", i2c(k));
else
printf("%c is the counterfeit coin and it is heavy.\n", i2c(k));
}
} return 0;
}
/**************************************************************
Problem: 1150
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/
九度OJ 1150:Counterfeit Dollar(假美元) (分析、检验)的更多相关文章
- 【九度OJ】题目1040:Prime Number 解题报告
[九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
随机推荐
- Oracle SID爆破工具SidGuess
Oracle SID爆破工具SidGuess 在Oracle中,SID是System IDentifier的缩写.SID是一个数据库的唯一标识符.当用户希望远程连接Oracle数据库时,则需要知道 ...
- 关于bug的沟通
关于BUG的沟通 一个人要去做一件事情,一般来说是按照自己的意愿去做的,如果不是自己想做而是被要求这么做的话,心里一定会留下点不愉快,特别是那种有自信有自己主见的人,比如说开发人员,当测试人员发现一个 ...
- 【JSOI2007】文本生成器
用AC自动机处理所有了解的单词 显然,不能直接算,直接算的话,我们需要大力容斥,复杂度不允许 我们不妨反过来做,我们根据AC自动机处理出所有的不可行解,然后用总数减去即可 计算所有不可行解用dp,\( ...
- 设计模式(1)---Factory Pattern
针对的问题:定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行. 第一步:创建接口 //创建一个接口 public interface Shape { pu ...
- DotnetBrowser高级教程-(4)使用MVC框架2-接收与返回模型
在上一节,我们搭建了基本的mvc框架,这一节,我们将实现数据的接受与返回,具体操作如下: 1.新建Model目录,新增模型类Person.cs,代码如下: public class Person { ...
- EasyMvc入门教程-基本控件说明(3)时间线
我们有时候经常看到如下的页面: 或者快递物流信息图标,那么利用EasyMvc如何实现呢?很简单,看下面的例子: @{ var data=new List<TimeLineItem>() { ...
- linuxshell编程之变量
变量分类: 用户自定义变量:局部变量 定义格式:变量名=变量值(*等号左右不能有空格,加了空格会报错) 调用格式:echo $变量名(调用的是变量名等效的值) 变量叠加:$x=123,y=" ...
- CentOS---网络配置具体解释
一.配置文件具体解释 在RHEL或者CentOS等Redhat系的Linux系统里.跟网络有关的主要设置文件例如以下: /etc/host.conf 配置域名服务client的控制文件 ...
- java -jar xxx.jar
之前用MyEclipse做了一个可执行jar,点击就可运行的. 今天突然不好用了,错误是: could not find the main class C:\123\abc.jar.Program w ...
- 一个免费的API-手机号码归属地接口
手机号码归属地接口:根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 .城市.区号.邮编.运营商和卡类型. 接口文档:https://www.juhe.cn/docs/api/id/1 ...