POJ1013称硬币【枚举】
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 52474 | Accepted: 16402 |
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<iostream>
#include<string.h>
using namespace std;
char cleft[3][7];//一共12个硬币,每次一边最多6个
char cright[3][7];
char result[3][7];
int num;
bool isFake(char c,bool heavy);//不加括号体
int main()
{ cin >> num;
while(num-- !=0)
{
for(int i = 0;i<3;i++)
{
cin >> cleft[i] >>cright[i]>>result[i];
}
for(char i = 'A';i<='L';++i)
{
if(isFake(i,true))
{
cout <<i<<" is the counterfeit coin and it is heavy. "<<endl;
break;
}
else if(isFake(i,false))
{
cout <<i<<" is the counterfeit coin and it is light. "<<endl;
break;
}
}
}
return 0;
} bool isFake(char c,bool heavy)
{
for(int i = 0;i<3;++i)
{
switch(result[i][0])
{
case 'u':
if(heavy == true && strchr(cleft[i],c)==NULL )//重 假币肯定在左边
return false;
if(heavy == false && strchr(cright[i],c)==NULL )//轻 假币肯定在右边
return false;
break;
case 'd':
if(heavy == true && strchr(cright[i],c)==NULL )//假币肯定在右边,并且重
return false;
if(heavy == false && strchr(cleft[i],c)==NULL )//假币肯定在左边,并且轻
return false;
break;
case 'e':
if(strchr(cleft[i],c)!=NULL || strchr(cright[i],c)!=NULL )//假币不在两边
return false;
break;
}
}
return true;
}
strchr语法:
#include <string.h>
char *strchr( const char *str, int ch );
功能:函数返回一个指向str 中ch 首次出现的位置,当没有在str 中找ch到返回NULL。
POJ1013称硬币【枚举】的更多相关文章
- POJ1013 称硬币
题目链接:http://poj.org/problem?id=1013 题目大意 有12枚硬币.其中有11枚真币和1枚假币.假币和真币重量不同,但不知道假币比真币轻还是重.现在,用一架天平称了这些币三 ...
- 枚举-称硬币POJ1013
#include <iostream> #include<string.h> using namespace std; char Lleft[][]; char Lright[ ...
- 算法题----称硬币: 2n(并不要求n是2的幂次方)个硬币,有两个硬币重量为m+1, m-1, 其余都是m 分治 O(lgn)找出假币
Description: 有2n个硬币和一个天平,其中有一个质量是m+1, 另一个硬币质量为m-1, 其余的硬币质量都是m. 要求:O(lgn)时间找出两枚假币 注意: n不一定是2的幂次方 算法1: ...
- 洛谷P1441 砝码称重 枚举 + 01背包
显然,n<=20, m<=4 的数据范围一眼爆搜. 直接搜索一下不用哪4个砝码,再做一遍01背包即可. 可能是本人太菜鸡,01背包部分调了半天QAQ-- #include<cstdi ...
- C++基础算法学习——猜假币
有12枚硬币.其中有11枚真币和1枚假币.假币和真币重量不同,但不知道假币比真币轻还是重.现在,用一架天平称了这些币三次,告诉你称的结果,请你找出假币并且确定假币是轻是重(数据保证一定能找出来).例题 ...
- 2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)
心得: 这比赛真的是不要不要的,pending了一下午,也不知道对错,直接做过去就是了,也没有管太多! Problem A: 两只老虎 Description 来,我们先来放松下,听听儿歌,一起“唱” ...
- IOS 枚举 enum
前言:oc中枚举的正确使用,可以增强代码的可读性,减少各种“错误”,让代码更加的规范.下面先介绍枚举的用法,最后介绍个人对枚举的理解,什么是枚举,为什么用枚举. 一. OC中,枚举的使用 1. 写法1 ...
- (BruteForce)暴力破解经典题目总结
在算法竞赛中,很多问题是来不及用数学公式推导出来的.或者说根本就找不到数学规律,这时我们就需要使用枚举来暴力破解. 不过枚举也是需要脑子的,一味的暴力只能超时.因此我这里选择了几道mooc上经典的题目 ...
- ACM训练计划建议(写给本校acmer,欢迎围观和指正)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
随机推荐
- B - Crossword solving
Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he ...
- 字体使用sp、dp的区别
Android设置字体大小, 该用sp还是dp? 大部分人肯定脱口而出, 用sp啊, 傻瓜都知道要用sp而不是dp!!! 那么为什么呢? 可能有人会说, 是google官方专门定义了sp这个单位来描述 ...
- [Usaco2006 Open]County Fair Events 参加节日庆祝
Description Farmer John has returned to the County Fair so he can attend the special events (concert ...
- 牛客小白月赛5-J-时间(time) (简单模拟)
题目描述 Apojacsleam是一个喜欢特殊时刻的人. 他定义了一个时刻,若电子表显示ab:ba(24小时制),则该时刻为“回文时刻”(可以有前导零).例如00:00就是回文时刻. 给定一个时刻,求 ...
- 214 Shortest Palindrome 最短回文串
给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...
- win7 系统 右键很慢
一般都是显卡驱动造成的, 在桌面按右键反应慢,通常都是显卡驱动程序惹的祸,最最简单有效的办法就是:开始--运行--. 运行regsvr32 /u igfxpph.dll
- php数组转为字符串,数据库存储
php对象转字符存储数据库的方法. 总所周知对象是不能直接存储到数据库的.那么我们用什么样的方法能够存储到数据库中能? 方法一:序列化serialize和unserialize 序列化对象serial ...
- Flutter交互实战-即刻App探索页下拉&拖拽效果
前言 Flutter最近比较热门,但是Flutter成体系的文章并不多,前期避免不了踩坑:我这篇文章主要介绍如何使用Flutter实现一个比较复杂的手势交互,顺便分享一下我在使用Flutter过程中遇 ...
- 学习笔记 第七章 使用CSS美化超链接
第7章 使用CSS美化超链接 学习重点 认识超链接 熟悉伪类 定义超链接样式 能够灵活设计符合页面风格的链接样式 7.1 定义超链接 在HTML5中建立超链接需要两个要素:设置为超链接的网页元素和 ...
- .Net Mvc EasyUI DataGrid 分页
由于项目的需要,最近一直在学习 .net MVC 和EasyUI.上周写了一个<.Net Mvc 返回Json,动态生成EasyUI Tree>,今天再写一个EasyUI中另一个重要的组件 ...