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的更多相关文章

  1. 备战NOIP每周写题记录(一)···不间断更新

    ※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...

  2. Java开发中的23种设计模式详解

    [放弃了原文访问者模式的Demo,自己写了一个新使用场景的Demo,加上了自己的理解] [源码地址:https://github.com/leon66666/DesignPattern] 一.设计模式 ...

  3. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

  4. CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator

    CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator 我还没有用过Compute Shader,所以现在把红宝书里的例子拿来了,加入CSharpGL中. ...

  5. ABP(现代ASP.NET样板开发框架)系列之23、ABP展现层——异常处理

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之23.ABP展现层——异常处理 ABP是“ASP.NET Boilerplate Project (ASP.NET ...

  6. Java开发中的23种设计模式详解(转)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  7. C#得到某月最后一天晚上23:59:59和某月第一天00:00:00

    项目需求: 某学校订单截止操作时间的上一个月最后一天晚上23:59:59 为止所有支付的订单统计: 代码: /// <summary> /// 通过学校和截止时间得到订单 /// < ...

  8. C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试

    在上篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及对应的对象模型,本篇继续微信小店的主题,介绍其中API接口的封装和测试使用.微信小店的相 ...

  9. [转载]IIS7报500.23错误的解决方法

    原文出处: 原文作者:pizibaidu 原文链接:http://pizibaidu.blog.51cto.com/1361909/1794446 背景:今天公司终端上有一个功能打开异常,报500错误 ...

随机推荐

  1. atitit.数据验证--db数据库数据验证约束

    atitit.数据验证--db数据库数据验证约束 1. 为了加强账户数据金额的安全性,需要增加验证字段..1 2. 创建帐户1 3. 更改账户2 4. ---code3 5. --fini4 1. 为 ...

  2. Python学习02 列表 List

    Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...

  3. Ubuntu Server 14.04升级Ubuntu Server 16.04

    Ubuntu Server 14.04升级Ubuntu Server 16.04 :转 http://blog.csdn.net/chszs 1.终端下执行命令 $ sudo apt-get upda ...

  4. Android 手机卫士--自定义控件(获取焦点的TextView)

    本文地址:http://www.cnblogs.com/wuyudong/p/5906735.html,转载请注明源地址. 本文将实现标题栏下面的textview中的文字跑马灯的效果,就是将一行文字水 ...

  5. jetty for linux 启用日志

    jetty7.8 文档 :https://wiki.eclipse.org/Jetty jetty9 文档: http://www.eclipse.org/jetty/documentation/cu ...

  6. 基于分布式、服务化的maven项目文件规划

    引言 此文不是纯粹介绍maven概念,而是介绍一个具体的maven项目文件规划 这个规划可能适合于研发比较复杂的业务,这些业务有分布式和服务化的需要. 这个规划能够解决因为分布式和服务化要求而引起的项 ...

  7. android 基本布局(RelativeLayout、TableLayout等)使用方法及各种属性

        本文介绍 Android 界面开发中最基本的四种布局LinearLayout.RelativeLayout.FrameLayout.TableLayout 的使用方法及这四种布局中常用的属性. ...

  8. Mongodb Manual阅读笔记:CH4 管理

    4 管理 Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mongodb Manual阅读笔 ...

  9. python中set集合

    一.set集合的特性 访问速度快 天生解决重复问题 二.set变量申明 s1 = set() s2 = set([1,2,3]) 备注:第二种方式在set类中直接传入一个序列. 三.set类中方法大全 ...

  10. mysql学习笔记(一)

    my建表操作 创建表 create Table <表名> ( 字段名1,数据类型 [列级约束] [默认值], 字段名2,数据类型 [列级约束] [默认值], ... [表级约束], [co ...