UVA - 817According to Bartjens
题意:给出一串数字,向里面插入“*”,“+”,“-”,使式子等于2000,至少要插入一个运算符。
分析:每个位置有4种情况不插入,“*”,“+”,“-”,最多有9个位置,每次枚举位置,最后判断输出。
# include<iostream>
# include<cstdio>
# include<cmath>
# include<map>
# include<queue>
# include<string>
# include<string.h>
#include<set>
#include<list>
# include<algorithm>
using namespace std;
int a[20];
char op[] = { '*','+','-' };
int len;
int res[20];
int reslen;
int ok = 0;
set<string>mp;
int ctoin(int s,int e) {int sum =a[s];for (int i = s+1; i <= e; i++) {sum = sum * 10 + a[i];}return sum;
}int judge() {int ans = 0; int cnum[20]; int ncount = 0;int res1[20]; int res1len = 0;cnum[0] = a[0];for (int i = 0; i < len; i++) {if (res[i] == 3) {if (cnum[ncount] == 0)return 0;cnum[ncount] = cnum[ncount] * 10 + a[i+1];}else {cnum[++ncount] = a[i + 1];res1[res1len++] = res[i];}}int cnum2[20]; int ncount2 = 0;cnum2[0] = cnum[0];int res2[20]; int res2len = 0;for (int i = 0; i < res1len; i++) {//乘法优先if (res1[i] == 0) {cnum2[ncount2] *= cnum[i + 1];}else {res2[res2len++] = res1[i];cnum2[++ncount2] = cnum[i + 1];}}ans = cnum2[0];for (int i = 0; i < res2len; i++) {if (res2[i] == 1)ans += cnum2[i + 1];elseans -= cnum2[i + 1];}return ans;
}
void dfs(int pos) {if (pos == len) {if (judge() == 2000) {ok = 1;char ttt[20] = { 0 };ttt[0]= a[0]+'0';int k = 0;for (int j = 0; j < len; j++) {if (res[j] == 3) {ttt[++k]= a[j+1]+'0';}else {ttt[++k]= op[res[j]];ttt[++k]= a[j + 1]+'0';}}mp.insert(ttt);}return;}for (int i = 0; i < 4; i++) {res[pos] = i;dfs(pos + 1);}}
void solve() {if (ctoin(0, len) == 2000) {cout << " IMPOSSIBLE" << endl;return;}dfs(0);if(!ok)cout << " IMPOSSIBLE" << endl;else {for (set<string>::iterator it = mp.begin(); it != mp.end(); ++it) {cout << " " << (*it) << "=";cout << endl;}}
}
int main() {char k;int kase = 0;while (cin >> k && k != '=') {a[0] = k - '0'; len = 0; mp.clear();ok = 0;while (cin >> k && k != '=') {a[++len] = k-'0';}cout << "Problem " << ++kase<<endl;solve();}
}
UVA - 817According to Bartjens相关推荐
- [搜索]UVa 129 困难的串
题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...
- uva 401.Palindromes
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Uva 3767 Dynamic len(set(a[L:R])) 树套树
Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...
- UVA 11752 超级幂
UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & ...
- UVa 11174 - Stand in a Line
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10112 - Myacm Triangles
UVa第一卷最后一题. 求内部不含点并且面积最大的三角形. 暴力. 代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #inclu ...
- UVa 10180 - Rope Crisis in Ropeland!
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=41&pa ...
- Uva 10074【递推dp】
UVa 10074 题意:求01矩阵的最大子0矩阵. http://www.csie.ntnu.edu.tw/~u91029/MaximumSubarray.html#2 这里说的很清楚.先求Larg ...
- UVA 116 Unidirectional TSP DP
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&p ...
最新文章
- Linux下安装mysql遇到的一些问题及解决办法
- mysql 实现nextval_mysql实现nextVal功能
- 生信入门-爱课程上的华中农业大学
- 腾讯云发布第三代云服务器矩阵,开放更强计算力赋能产业智能化
- Apahce服务器配置非根目录的虚拟目录
- 成都Uber优步司机奖励政策(1月26日)
- java游戏鬼吹灯安卓版下载_鬼吹灯手机游戏下载
- windsns社交门户系统源码v1.0-掌上移动社交类SNS网站系统源码
- html5 canvas 一个漫天飞雪的效果
- R语言NBA球员数据挖掘简单实现
- webpack 热更新
- windows下安装speedtest的经历和问题
- 无责任猜想#冰桶挑战#大佬们相爱相杀众生相
- 《Hive权威指南》第六章:查询
- linux命令行开头是sh,shell脚本语言的使用(超全超详细)
- Python斐波那契数列算法
- 看mysql安装在哪怎么看_mysql安装在哪儿怎么查看?
- 校验手机号和获取验证码
- 80后的麦当劳如何俘获年轻人的心
- Field xxxMapper in xxxServiceImpl required a bean of type XxxMapper解决方法