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错误 ...
随机推荐
- Android Tips: 在给drawable中添加图片资源时,文件名必须全小写
在给drawable中添加图片资源时,文件名必须全小写
- Swift设置自动行高
// 设置行高自适应 tableView.rowHeight = UITableViewAutomaticDimension // 设置预估行高 tableView.estimatedRowHeigh ...
- UITableView增加和删除、移动
复习一下: 1.在控制器上添加一个UITableView, 暂时该UITableView控件变量名命名为为tableView, 设置控件代理,实现控制器的UITableViewDataSource, ...
- 如何优化TableView
关于UITable的优化: 1.最常用的就是不重复生成单元格,很常见,很实用: 2.使用不透明的视图可以提高渲染速度,xCode中默认TableCell的背景就是不透明的: 3.如果有必要减少视图中的 ...
- cocoapods遇到的问题 (pod: command not found的问题)
在使用CocoaPod为项目添加第三方类库时,出现了-bash: pod: command not found的问题: 在网上看到了一位哥的方法:确实有效:
- 1.9 基础知识——GP2.10 高级别的领导检查(Higher level management)
GP2.10 Review the activities,status,and results of XXX process with highter level management and res ...
- Atitit.论图片类型 垃圾文件的识别与清理 流程与设计原则 与api概要设计 v2 pbj
Atitit.论图片类型 垃圾文件的识别与清理 流程与设计原则 与api概要设计 v2 pbj 1. 俩个问题::识别垃圾文件与清理策略1 2. 如何识别垃圾图片1 2.1. 体积过小文件<1 ...
- #winhec# 开发人员刷屏看点 (视频)
今天大家已经被winhec刷屏了,本来不想写这篇了,但看了所有的文章,大家关注的都是windows 10的那些新功能,小米win10刷机,联想千元手机,小娜啥的.对于keynote上第二部分 Don ...
- (视频) 开源,免费和跨平台 - MVP ComCamp 2015 KEYNOTE
2015年1月31日,作为KEYNOTE演讲嘉宾,我和来自全国各地的开发人员分享了作为一名MVP的一些体会. Keynote – Open Source, Free Tools and Cross P ...
- SAM4E单片机之旅——24、使用DSP库求向量数量积
DSP(Digital Signal Processing,数字信号处理)中会使用大量的数学运算.Cortex-M4中,配置了一些强大的部件,以提高DSP能力.同时CMSIS提供了一个DSP库,提供了 ...
