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 ...
随机推荐
- Harbor 企业级镜像仓库搭建
Habor是由VMWare公司开源的容器镜像仓库.事实上,Habor是在Docker Registry上进行了相应的 企业级扩展,从而获得了更加广泛的应用,这些新的企业级特性包括:管理用户界面,基于角 ...
- Java异常类层次结构图
1. 分类图镇楼: 2.运行时异常与非运行时异常区别: Java 提供了两类主要的异常 :runtime exception 和 checked exception. 2.1 checked exce ...
- SOA和Web Servcie的区别
soa(Service-Oriented Architecture)是服务对服务的,web service是服务对客户端的. 都提供服务. 服务接口都是基于开发的. 服务接口和服务的具体实现都是分 ...
- Codeforces.314E.Sereja and Squares(DP)
题目链接 http://www.cnblogs.com/TheRoadToTheGold/p/8443668.html \(Description\) 给你一个擦去了部分左括号和全部右括号的括号序列, ...
- iOS 七牛多张图片上传
-(void)uploadImages:(NSArray *)images atIndex:(NSInteger)index token:(NSString *)token uploadManager ...
- logstash grok 分割匹配日志
使用logstash的时候,为了更细致的切割日志,会写一些正则表达式. 使用方法 input { file { type => "billin" path => &qu ...
- Entity framework 增加默认执行时间
public partial class ProductionSupportEntities : DbContext { public ProductionSupportEntities() : ba ...
- 搭建《深入Linux内核架构》的Linux环境
作者 彭东林 pengdonglin137@163.com 软件 Host: Ubuntu14.04 64 Qemu 2.8.0 Linux 2.6.24 busybox 1.24.2 gcc 4.4 ...
- ASP.NET MVC异步验证是如何工作的03,jquery.validate.unobtrusive.js是如何工作的
在上一篇"ASP.NET MVC异步验证是如何工作的02,异步验证表单元素的创建"中了解了ASP.NET异步验证是如何创建表单元素的,本篇体验jquery.validate.uno ...
- 微信破解,解密?How To Decrypt WeChat EnMicroMsg.db Database?
20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送) 国内私募机构九鼎控股打造,九鼎投资是在全国股 ...