uva 10344 23 out of 5 凑运算结果 全排列+dfs
五个数三个运算符号,排列之后凑成结果为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的更多相关文章
- 牛客练习赛 23 C 托米的位运算
链接:https://www.nowcoder.com/acm/contest/156/C来源:牛客网 托米完成了1317的上一个任务,十分高兴,可是考验还没有结束 说话间1317给了托米 n 个自然 ...
- Uva 12169 不爽的裁判 模运算
题目链接:https://vjudge.net/contest/156903#problem/B 题意: 有一个递推公式 : a,b都不是已知的,给出了 x1,x3,x5.... 求x2,x4,x6. ...
- UVA 213 信息解码(二进制&位运算)
题意: 出自刘汝佳算法竞赛入门经典第四章. 考虑下面的01串序列: 0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, …, 1 ...
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...
- Java实现蓝桥杯凑算式(全排列)
题目6.凑算式 凑算式 B DEF A + - + ------- = 10 C GHI (如果显示有问题,可以参见[图1.jpg]) 这个算式中AI代表19的数字,不同的字母代表不同的数字. 比如: ...
- Codeforces986C AND Graph 【位运算】【dfs】
题目大意: 一张$ m $个编号互异点图,最大不超过$ 2^n $,若两个编号位与为0则连边,问连通块数量. 题目分析: 考虑怎样的两个点会连边.这种说法对于A和B两个点来说,就相当于B在A的0的子集 ...
- uva 331 Mapping the Swaps 求交换排序的map 纯DFS
给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序. n不大于5,用dfs做. 代码: #include <cstdio> #include <cstring> # ...
- uva10344 23 out of 5
Your task is to write a program that can decide whether you can nd an arithmetic expression consisti ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
随机推荐
- JdbcTemplate使用小结
org.springframework.jdbc.core.JdbcTemplate.query(String sql, Object[] args, RowMapper<StaffUnionV ...
- Ubuntu 18.04 更新源
[原因] 使用国外的源,在更新软件的时候会很慢,换成国内的源会快很多. [命令] 1.备份源文件 sudo cp /etc/apt/sources.list /etc/apt/sources.list ...
- LOJ.121.[离线可过]动态图连通性(线段树分治 按秩合并)
题目链接 以时间为下标建线段树.线段树每个节点开个vector. 对每条边在其出现时间内加入线段树,即,把这条边按时间放在线段树的对应区间上,会影响\(O(\log n)\)个节点. 询问就放在线段树 ...
- hdu 1507 记录路径的二分匹配 **
题意:N*M的矩形,向其中填充1*2的小块矩形,黑色的部分不能填充,问最多可以填充多少块.链接:点我 黑白棋最大匹配 将棋盘中i+j为奇数的做A集合,偶数的做B集合,相邻的则建立联系.于是便转换成寻找 ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem D. Grumpy Cat 交互题
Problem D. Grumpy Cat 题目连接: http://www.codeforces.com/gym/100253 Description This problem is a littl ...
- hdu 5792 World is Exploding 树状数组
World is Exploding 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- [原创]SOAPUI工具介绍
[原创]SOAPUI工具介绍 一 官方网站:http://www.soapui.org/二 下载地址:http://sourceforge.net/projects/soapui/files/三 so ...
- USBDM RS08/HCS08/HCS12/Coldfire V1,2,3,4/DSC/Kinetis Debugger and Programmer -- BDM Construction and Firmware
Construction. Build the hardware using the information provided in the PCB download. The following a ...
- 搭建基于crtmpserver的点播解决方案
1. linux环境下build并启动crtmpserver 这部分可以参见我写的专项详解文章 <crtmpserver流媒体服务器的介绍与搭建> 和 <crtmpserver配置文 ...
- Leetcode 234 Palindrome Linked List 复杂度为时间O(n) 和空间(1)解法
1. 问题描写叙述 给定一个单链表,推断其内容是不是回文类型. 比如1–>2–>3–>2–>1.时间和空间复杂都尽量低. 2. 方法与思路 1)比較朴素的算法. 因为给定的数据 ...