uva10344 23 out of 5
Your task is to write a program that can decide whether you can nd an arithmetic expression consisting
of ve given numbers ai (1 i 5) that will yield the value 23.
For this problem we will only consider arithmetic expressions of the following from:
(((a(1) o1 a(2)) o2 a(3)) o3 a(4)) o4 a(5)
where : f1;2;3;4;5g ! f1;2;3;4;5g is a bijective function and oi 2 f+; ;g(1 i 4)
Input
The Input consists of 5-Tupels of positive Integers, each between 1 and 50.
Input is terminated by a line containing ve zero's. This line should not be processed. Input le
will have no more than 25 lines.
Output
For each 5-Tupel print `Possible' (without quotes) if their exists an arithmetic expression (as described
above) that yields 23. Otherwise print `Impossible'.
Sample Input
1 1 1 1 1
1 2 3 4 5
2 3 5 7 11
0 0 0 0 0
Sample Output
Impossible
Possible
Possible
题目大意如下:给定5个正整数,和一个算术集合{+,-,*},求23点。
思路:因为24点或23点或n点的计算公式为(((a(1) o1 a(2)) o2 a(3)) o3 a(4)) o4 a(5),可知需要在5个正整数中选定一个初始的数来作为基础值,所以枚举此数,再进行回溯。
| 6748808 |
Accepted
|
470 | 698 |
2016-08-04 20:58:14
|
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int a[6];
bool vis[6];
bool read()
{
bool useable=0;
for(int i=0;i<5;i++){
scanf("%d",&a[i]);
if(a[i])useable=1;
}
return useable;
}
bool dfs(int cur,int ans)
{
if(cur==4&&ans==23)return 1;
for(int i=0;i<5;i++)
if(!vis[i]){
vis[i]=1;
if(dfs(cur+1,ans+a[i]))return 1;
if(dfs(cur+1,ans*a[i]))return 1;
if(dfs(cur+1,ans-a[i]))return 1;
vis[i]=0;
}
return 0;
}
bool solve()
{
for(int i=0;i<5;i++){
memset(vis,0,sizeof(vis));
vis[i]=1;
if(dfs(0,a[i]))return 1;
vis[i]=0;
}
return 0;
}
int main()
{
while(read()){
if(solve())puts("Possible");
else puts("Impossible");
}
return 0;
}
uva10344 23 out of 5的更多相关文章
- 备战NOIP每周写题记录(一)···不间断更新
※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...
- Java开发中的23种设计模式详解
[放弃了原文访问者模式的Demo,自己写了一个新使用场景的Demo,加上了自己的理解] [源码地址:https://github.com/leon66666/DesignPattern] 一.设计模式 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator
CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator 我还没有用过Compute Shader,所以现在把红宝书里的例子拿来了,加入CSharpGL中. ...
- ABP(现代ASP.NET样板开发框架)系列之23、ABP展现层——异常处理
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之23.ABP展现层——异常处理 ABP是“ASP.NET Boilerplate Project (ASP.NET ...
- Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- C#得到某月最后一天晚上23:59:59和某月第一天00:00:00
项目需求: 某学校订单截止操作时间的上一个月最后一天晚上23:59:59 为止所有支付的订单统计: 代码: /// <summary> /// 通过学校和截止时间得到订单 /// < ...
- C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试
在上篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及对应的对象模型,本篇继续微信小店的主题,介绍其中API接口的封装和测试使用.微信小店的相 ...
- [转载]IIS7报500.23错误的解决方法
原文出处: 原文作者:pizibaidu 原文链接:http://pizibaidu.blog.51cto.com/1361909/1794446 背景:今天公司终端上有一个功能打开异常,报500错误 ...
随机推荐
- 【Openlayers3】在地图上添加highcharts图表
今天试用了一下ol3,效果很是不错! ol3中有个ol.Overlay,使用这个类我们可以在地图中添加各种html要素. 下面我们在地图中添加一个饼图 html中添加一个div容器: <div ...
- Java虚拟机JVM学习05 类加载器的父委托机制
Java虚拟机JVM学习05 类加载器的父委托机制 类加载器 类加载器用来把类加载到Java虚拟机中. 类加载器的类型 有两种类型的类加载器: 1.JVM自带的加载器: 根类加载器(Bootstrap ...
- iOS开发之图片分辨率与像素对齐
像素对齐的概念 在iOS中,有一个概念叫做像素对齐,如果像素不对齐,那么在GPU渲染时,需要进行插值计算,这个插值计算的过程会有性能损耗. 在模拟器上,有一个选项可以把像素不对齐的部分显示出来.  ...
- <转>iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!
原文地址:http://blog.csdn.net/xiaominghimi/article/details/6937097 //——2012-12-11日更新 获取"产品付费数量等于0 ...
- java 实现 LINQ 的一些框架记录一下
jOOQ: http://www.jooq.org JINQ: http://www.jinq.org JaQue: http://github.com/TrigerSoft/jaque JaQu: ...
- java url方法解释
java 的url类中有很多get方法 以下是获取值的意义 // 首先先看一下wikipedia上关于url的一个描述 //Every HTTP URL conforms to the syntax ...
- 你真的了解UIGestureRecognizer吗?
一:首先查看一下关于UIGestureRecognizer的定义 //当前手势状态 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { //尚 ...
- OC 中的block存储位置
以下所有在ARC情况下: 一.block块的存储位置(block块入口地址):可能存放在2个地方:代码区.堆区(程序分5个区,还有常量区.全局区和栈区,对于MRC情况下代码还可能存在栈区.关于分区详细 ...
- android [因为开了刷机精灵等软件 导致adb 无法使用]error: could not install *smartsocket* listener: cannot bind
今天 使用 刷机精灵后 在使用android studio 时发现 adb 无法正常使用. 于是 想重启 adb.exe , 直接在DOS里杀掉adb输入:adb kill-server 再启动输 ...
- Enabling Cross-Origin Requests in ASP.NET Web API 2
Introduction This tutorial demonstrates CORS support in ASP.NET Web API. We’ll start by creating two ...
