北大poj- 1013
Counterfeit Dollar
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 50515 | Accepted: 15808 |
Description
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.
Input
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.
Output
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.
Sample Input
1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
Sample Output
K is the counterfeit coin and it is light.
Source
#include <stdio.h>
#include <string.h>
#include <stdlib.h> #define TRUE (int)1
#define FALSE (int)0 #define CASE_NUM 3
#define MAX_COIN_NUM 12 typedef int BOOL; typedef struct
{
int strLen;
char str1[MAX_COIN_NUM+];
char str2[MAX_COIN_NUM+];
char delta[];
}WeightCase; WeightCase g_case[CASE_NUM];
BOOL g_isEvenCoin[MAX_COIN_NUM];
char g_coin[MAX_COIN_NUM]; const char up[] = "up\0";
const char down[] = "down\0";
const char even[] = "even\0"; void Input()
{
int i = ;
for(i = ; i < CASE_NUM; i++)
{
scanf(" %s", g_case[i].str1);
scanf(" %s", g_case[i].str2);
scanf(" %s", g_case[i].delta);
g_case[i].strLen = strlen(g_case[i].str1);
}
} void ProcEvenCase()
{
int i, j; for(i = ; i < CASE_NUM; i++)
{
if( != strcmp(g_case[i].delta, even)) continue; for(j = ; j < g_case[i].strLen; j++)
{
g_isEvenCoin[g_case[i].str1[j] - 'A'] = TRUE;
g_isEvenCoin[g_case[i].str2[j] - 'A'] = TRUE;
}
}
} void ProcOtherCase()
{
int i, j, delta; for(i = ; i < CASE_NUM; i++)
{
if( == strcmp(g_case[i].delta, even)) continue; if( == strcmp(g_case[i].delta, up))
delta = ;
else
delta = -; for(j = ; j < g_case[i].strLen; j++)
{
if(!g_isEvenCoin[g_case[i].str1[j] - 'A']) g_coin[g_case[i].str1[j] - 'A'] += delta;
if(!g_isEvenCoin[g_case[i].str2[j] - 'A']) g_coin[g_case[i].str2[j] - 'A'] -= delta;
}
}
} void Proc()
{
memset(g_coin, , sizeof(g_coin));
memset(g_isEvenCoin, FALSE, sizeof(g_isEvenCoin)); ProcEvenCase();
ProcOtherCase();
} void Output()
{
int i, tmp, pos = , max = ;
char coin;
for(i = ; i < sizeof(g_coin); i++)
{
tmp = abs(g_coin[i]);
if(tmp > max)
{
max = tmp;
pos = i;
}
}
coin = pos+'A'; if(g_coin[pos] < )
printf("%c is the counterfeit coin and it is light.\n", coin);
else
printf("%c is the counterfeit coin and it is heavy.\n", coin);
} int main()
{
int num = ;
scanf("%d", &num);
while(num--)
{
Input();
Proc();
Output();
} return ;
}
北大poj- 1013的更多相关文章
- 北大POJ题库使用指南
原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...
- Poj 1013 Counterfeit Dollar / OpenJudge 1013(2692) 假币问题
1.链接地址: http://poj.org/problem?id=1013 http://bailian.openjudge.cn/practice/2692 http://bailian.open ...
- 模拟,找次品硬币,Counterfeit Dollar(POJ 1013)
题目链接:http://poj.org/problem?id=1013 解题报告: 1.由于次品的重量不清楚,用time['L'+1]来记录各个字母被怀疑的次数.为负数则轻,为正数则重. 2.用zer ...
- POJ 1013 Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36206 Accepted: 11 ...
- POJ 1013 Counterfeit Dollar 集合上的位运算
Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...
- POJ 1013 小水题 暴力模拟
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35774 Accepted: 11 ...
- poj 1013(uva 608) Counterfeit Dollar
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #in ...
- POJ 1013
#include"string.h"char left[3][7],right[3][7],result[3][5];bool isHeavy(char x ){ int i ...
- POJ 1013:Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42028 Accepted: 13 ...
- 思维+模拟--POJ 1013 Counterfeit Dollar
Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver d ...
随机推荐
- erlang下lists模块sort(排序)方法源码解析(二)
上接erlang下lists模块sort(排序)方法源码解析(一),到目前为止,list列表已经被分割成N个列表,而且每个列表的元素是有序的(从大到小) 下面我们重点来看看mergel和rmergel ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- Angular Js 与bootstrap, angular 与 vue.js
今天突然接到电话, 问我他们的区别 虽然平时看了,但是没记住,凉凉是肯定的 总结一下: bootstrap不算是javascript框架,它只是一个前端的ui框架,然后有一些附带的js插件而已.an ...
- echarts常用方法,饼图切换圆环中文字(三)
在echarts的饼图应用时,遇到过一个需求就是鼠标移到半环上可以切换环中的文字,同时支持legend点击事件.误区是,鼠标移动到环上重新渲染option,以切换内部的文字.重新渲染option的做法 ...
- python 数据分析基础
安装Python基础的几个数据分析库: pip install pandas pip install numpy pip install scipy pip install scikit-surpri ...
- java Arrays常用方法
1. 简介 Arrays类包含用于操作数组的各种方法(例如排序和搜索).此类还包含一个静态工厂,允许将数组视为列表. 如果指定的数组引用为null,则此类中的方法都抛出NullPointerExcep ...
- mockjs在vue中的使用
mockjs在vue中的使用 安装好vue-cli后 加载模块: npm install mockjs 创建mock.js文件到src目录下的任一合适文件内新建mockjs.js,设置好拦截信息,设置 ...
- Nginx 作用
django 请求的生命周期 Nginx 的作用: 浏览器 --- nginx(反向代理器)-- uwsgi --- django项目nginx : 负载均衡, 将任务分发给不同的uwsgi 动静分离 ...
- VS2017开发.net core 时默认发布路径文件夹多个BPC
新安装的VS2017,编译后进行发布,结果在bin文件夹下多了个BPC文件夹,很是费解,查了资料才知道是VS2017默认设置了环境变量.在此记录下,如果不需要默认路径可修改环境变量,具体操作如下: 我 ...
- PyCharm for windows 快捷功能(图片形式讲解)