五个数三个运算符号,排列之后凑成结果为23,不考虑优先级。

很水,数据量也不大,先生成五个数的全排列,用dfs找出结果能否为23即可。

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
#define RES 23 const int maxn = 5; int num[maxn];
bool flag; bool input(void) {
for (int i = 0; i < maxn; i++)
scanf("%d", &num[i]);
if (num[0] || num[1] || num[2] || num[3] || num[4])
return true;
return false;
} void dfs(int res, int d) {
if (flag) return;
if (d == maxn) {
if (res == RES)
flag = true;
return;
}
dfs(res + num[d], d + 1);
dfs(res - num[d], d + 1);
dfs(res * num[d], d + 1);
} int main() {
while (input()) {
sort (num, num + maxn);
flag = false;
do {
dfs(num[0], 1);
} while (!flag && next_permutation(num, num + maxn));
if (flag)
printf("Possible\n");
else
printf("Impossible\n");
}
return 0;
}

uva 10344 23 out of 5 凑运算结果 全排列+dfs的更多相关文章

  1. 牛客练习赛 23 C 托米的位运算

    链接:https://www.nowcoder.com/acm/contest/156/C来源:牛客网 托米完成了1317的上一个任务,十分高兴,可是考验还没有结束 说话间1317给了托米 n 个自然 ...

  2. Uva 12169 不爽的裁判 模运算

    题目链接:https://vjudge.net/contest/156903#problem/B 题意: 有一个递推公式 : a,b都不是已知的,给出了 x1,x3,x5.... 求x2,x4,x6. ...

  3. UVA 213 信息解码(二进制&位运算)

    题意: 出自刘汝佳算法竞赛入门经典第四章. 考虑下面的01串序列: 0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, …, 1 ...

  4. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

  5. Java实现蓝桥杯凑算式(全排列)

    题目6.凑算式 凑算式 B DEF A + - + ------- = 10 C GHI (如果显示有问题,可以参见[图1.jpg]) 这个算式中AI代表19的数字,不同的字母代表不同的数字. 比如: ...

  6. Codeforces986C AND Graph 【位运算】【dfs】

    题目大意: 一张$ m $个编号互异点图,最大不超过$ 2^n $,若两个编号位与为0则连边,问连通块数量. 题目分析: 考虑怎样的两个点会连边.这种说法对于A和B两个点来说,就相当于B在A的0的子集 ...

  7. uva 331 Mapping the Swaps 求交换排序的map 纯DFS

    给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序. n不大于5,用dfs做. 代码: #include <cstdio> #include <cstring> # ...

  8. uva10344 23 out of 5

    Your task is to write a program that can decide whether you can nd an arithmetic expression consisti ...

  9. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

随机推荐

  1. rabbitmq学习(八) —— 可靠机制上的“可靠”

    接着上一篇,既然已经有了手动ack.confirm机制.return机制,还不够吗? 以下博文转自https://www.jianshu.com/p/6579e48d18ae和https://my.o ...

  2. 循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 既然前后端 ...

  3. windows10 下安装tensorflow 并且在jupyter notebook 上使用tensorflow

    一.安装jupyter notebook并配置环境 首先建议大家安装anaconda,最新版本请到官网下载(点击下载连接),没错,直接点击下载python3.6版本的(当然选择做自己电脑相应的位数,我 ...

  4. Android通知栏沉浸式/透明化完整解决方案

    转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6640649.html 参考文献:https://github.com/ljgsonx/adaptiveSt ...

  5. @repository的含义,并且有时候却不用写,为什么?

    //最后发现是这样的:@repository跟@Service,@Compent,@Controller这4种注解是没什么本质区别,都是声明作用,取不同的名字只是为了更好区分各自的功能.下图更多的作用 ...

  6. python的异常处理及异常类定义

    python的异常处理语法和大多数语言相似: try: try块的语句... except exceptiontype1 as var:#使用as语句获得本次捕获到的异常的实例var except块语 ...

  7. hdu 5831 Rikka with Parenthesis II 线段树

    Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  8. NAS系统收集

    FreeNAS®,目前最受欢迎的开源免费 NAS 操作系统之一,基于以安全和稳定著称的 FreeBSD 系统开发,由 ixsystems 公司的技术团队维护.项目地址:www.freenas.org ...

  9. C++ Primer笔记3_默认实參_类初探_名字查找与类的作用域

    1.默认函数实參 在C++中,能够为參数指定默认值,C语言是不支持默认參数的,Java也不支持! 默认參数的语法与使用: (1)在函数声明或定义时,直接对參数赋值.这就是默认參数: (2)在函数调用时 ...

  10. JTAG Simplified

    JTAG Simplified So the other day, I explored the JTAG bus interface which is frequently found in CPL ...