uva 10700 Camel trading (水贪心+栈)
2015-12-04 20:03
351 查看
Camel trading
Time Limit: 1 secondBackground
Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had itsorigin in the financial accounts of a camel transaction. The formula lacked parenthesis and was
ambiguous. So, he decided to ask savants to provide him with a method to find which interpretation
is the most advantageous for him, depending on whether is is buying or selling the camels.
The Problem
You are commissioned by El Mamum to write a program that determines the maximum and minimumpossible interpretation of a parenthesis-less expression.
Input
The input consists of an integer N, followed by N lines, each containing an expression. Eachexpression is composed of at most 12 numbers, each ranging between 1 and 20, and separated by
the sum and product operators + and *.
Output
For each given expression, the output will echo a line with the corresponding maximal and minimalinterpretations, following the format given in the sample output.
Sample input
31+2*3*4+5
4*18+14+7*10
3+11+4*1*13*12*8+3*3+8
Sample output
The maximum and minimum are 81 and 30.The maximum and minimum are 1560 and 156.
The maximum and minimum are 339768 and 5023.
——————————————————————————————
——————————————————————————————
给你一个式子,只包含+和*还有数字,数字不大于20,数字个数不超过12个,可以自由添加括号,求这个式子可以得到的最大值和最小值
++++++++++++++++++++++++++++++++++++++++++++
水贪心,能看出来求最小就是不填括号按照优先级计算的结果,求最大就是把加法和乘法的优先级调换,即 求最大先加后乘,求最小先乘后加
可以用个栈来实现,代码简单不少——————————当然我一开始没想到………………
————————————————————————————————————————————————————————————
贴上矬代码(:зゝ∠)
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; char formula[1000000],sym[1000000]; int n,num[1000000],tail,sl,syl,nul; long long minimum,maximum,x; int nuk[10000],nl,k; long long mul[1000000],add[1000000]; int main() { scanf("%d%*c",&n); for (int i=1;i<=n;i++) { gets(formula); minimum=0; maximum=1; //printf("%s\n",formula); memset(num,0,sizeof(num)); memset(sym,'\0',sizeof(sym)); sl=strlen(formula); nl=0; nul=0; syl=0; for (int i=0;i<sl;i++) { if (formula[i]=='+'||formula[i]=='*') { sym[++syl]=formula[i]; nul++; k=1; while(nl) { num[nul]+=(nuk[nl]*k); k*=10; nl--; } nl=0; /*printf("%d",num[nul]); printf("%c",sym[syl]);*/ } else { nl++; nuk[nl]=formula[i]-48; } }; nul++; k=1; while(nl) { num[nul]+=(nuk[nl]*k); k*=10; nl--; } tail=0; add[++tail]=num[1]; for (int i=1;i<=syl;i++) { if (sym[i]=='+') add[++tail]=num[i+1]; if (sym[i]=='*') { x=add[tail--];//x会爆_(:зゝ∠)_开long long x=x*num[i+1]; add[++tail]=x;//add同理_(:зゝ∠)_ } } for (int i=1;i<=tail;i++) minimum+=add[i]; tail=0; mul[++tail]=num[1]; for (int i=1;i<=syl;i++) { if (sym[i]=='*') mul[++tail]=num[i+1]; if (sym[i]=='+') { x=mul[tail--]; x=x+num[i+1]; mul[++tail]=x; } } for (int i=1;i<=tail;i++) maximum*=mul[i]; printf("The maximum and minimum are %lld and %lld.\n",maximum,minimum); } }
相关文章推荐
- Thrift C++ Server demo 实现
- 二叉树的遍历
- bzoj1188 分裂游戏 博弈论
- 模拟也有动脑子,类似蛇形填数
- linux (Centos7) 编译安装nginx
- 四边形密铺平面
- HDU 1312 Red and Black --- 入门搜索 DFS解法
- 算法竞赛 之果园里的树
- Java查找数组中的最小值
- hdu2196 树形dp
- 青海湖
- ORA-00439: feature not enabled: Managed Standby
- HDU 5057 Argestes and Sequence 分块
- Matlab中画圆
- 检测调试器和trace
- 后台定位上传的代码实践
- 监督学习和无,监督学习区别
- V$SESSION_LONGOPS
- 开始了我的记录生活之旅!
- ext2/3文件系统存储,查找,新建,删除文件机制详解