无括号表达式
来源:互联网 发布:建筑效果图制作软件 编辑:程序博客网 时间:2024/05/29 06:40
Problem E - Camel trading
Time Limit: 1 second
Background
Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin 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 minimum possible interpretation of a parenthesis-less expression.
Input
The input consists of an integer N, followed by N lines, each containing an expression. Each expression is composed of at most 12numbers, 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 minimal interpretations, following the format given in the sample output.
Sample input
31+2*3*4+54*18+14+7*103+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.
主要是能想到,先按正常顺序算肯定最小,而在乘之前先吧能加在乘数上的数都加上就是最大的
#include <stdio.h>#include <string.h>#include <ctype.h>int main(void){char str[100];int n, len, i, top = 0;double stack[100];char opr;int head;double min, max, f, temp;scanf("%d", &n);while(n--){scanf("%s", str);len = (int)strlen(str);temp = 0.0;str[len] = '*';max = 1.0;head = 0;for(i = 0; i <= len; ++i){if(!isdigit(str[i])){opr = str[i];str[i] = '\0';sscanf(&str[head], "%lf", &f);str[i] = opr;temp += f;if(str[i] == '*'){max *= temp;temp = 0.0;}head = i + 1;}}temp = 0;top = 0;str[len] = '+';head = 0;min = 0.0;for(i = 0; i <= len; ++i){if(!isdigit(str[i])){opr = str[i];str[i] = '\0';sscanf(&str[head], "%lf", &f);str[i] = opr;stack[top++] = f;if(str[i] == '+'){temp = 1.0;while(top){temp *= stack[--top];}min += temp;}head = i + 1;}}printf("The maximum and minimum are %.0lf and %.0lf.\n", max, min);}return 0;}
- 无括号表达式
- 10700 无括号的表达式
- SICILY 中缀表达式转后缀表达式(无括号)
- uva10700 - Camel trading(无括号的表达式)
- UVA 10700 Camel trading 无括号的表达式 贪心
- 数据结构 栈 无括号算术表达式处理算法 C语言版
- 括号表达式
- 括号表达式
- 表达式求值(无括号、无空格、每个数0-9、有运算符优先级)
- 给一个无括号的布尔表达式求所有组合数
- 17、表达式中的括号
- FOJ 1706 括号表达式
- FZU 1706 括号表达式
- 表达式括号匹配
- 表达式括号匹配
- 表达式括号匹配
- stack(括号,表达式)
- 正则表达式中的括号
- Android实现网络多线程断点续传下载
- Objective-C的hook方案(一): Method Swizzling
- Golismero使用技巧汇总
- JS操作iframe
- SSH分层包框架图解
- 无括号表达式
- java.io.IOException: No FileSystem for scheme: hdfs
- web4
- 在使用reviewboard时,向分支添加新文件后,不能产生diff并由此就不能post-review新review request的原因
- 介绍Basic和Digest
- DG - 逻辑Standby创建
- mono touch 中UITextField和UILabel的一些你必须要知道的属性设置
- File-backed Storage Gadget
- Hadoop作业提交分析(二)