题意:给定一个表达式,只有+*,然后问你按照法则运算和从左到右计算结果有什么不同。

析:没什么可说的,直接算两次就好。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[maxn];
stack<char> mark;
stack<int> num;
int ans1, ans2; void solve(){
int n = strlen(s);
s[n] = '+';
s[n+1] = '0';
s[n+2] = 0;
n += 2;
int cnt = 0;
ans1 = 0;
vector<int> v1;
vector<char> v2;
for(int i = 0; i < n; ++i){
if(isalnum(s[i])) cnt = 10*cnt + s[i] - '0';
else{
v1.push_back(cnt);
v2.push_back(s[i]);
if(mark.empty()){
num.push(cnt);
mark.push(s[i]);
}
else{
char op = mark.top();
if(op == '*'){
int t = num.top(); num.pop();
t *= cnt;
num.push(t);
mark.pop();
}
else num.push(cnt);
mark.push(s[i]);
}
cnt = 0;
}
}
while(!num.empty()) ans1 += num.top(), num.pop();
ans2 = v1[0];
cnt = 0;
for(int i = 1; i < v1.size(); ++i)
if(v2[cnt++] == '*') ans2 *= v1[i];
else ans2 += v1[i];
} int main(){
while(scanf("%s", s) == 1){
scanf("%d", &n);
while(!mark.empty()) mark.pop();
solve();
if(ans1 == ans2 && ans1 == n) puts("U");
else if(ans1 != n && ans2 != n) puts("I");
else if(ans1 == n) puts("M");
else puts("L");
}
return 0;
}

UVaLive 6833 Miscalculation (表达式计算)的更多相关文章

  1. UVAlive 6833 Miscalculation 字符串处理

    去年省选的题 因为卡了这道题再加上队友占机时 省选第一天华丽爆零了 用事实证明了1+1+1<1的事实 毕竟下半年单挑了东北赛名额 省赛打不出来名额就真的就不怪我了(摔 现在有拿出来做 长个记性 ...

  2. 模拟/字符串处理 UVALive 6833 Miscalculatio

    题目传送门 /* 模拟/字符串处理:主要是对*的处理,先把乘的预处理后再用加法,比如说是:1+2*3+4 = 1+..6+4 = 11 */ #include <cstdio> #incl ...

  3. .NET平台开源项目速览(8)Expression Evaluator表达式计算组件使用

    在文章:这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,给大家初步介绍了一下Expression Evaluator验证组件.那里只是概述了一下,并没有对其使用和强大功能做 ...

  4. C# - 二叉树表达式计算

    很早以前就写过双栈的表达式计算. 这次因为想深入学一下二叉树,网上都是些老掉牙的关于二叉树的基本操作. 感觉如果就学那些概念,没意思也不好记忆.于是动手写了一个表达式计算的应用例子. 这样学习印象才深 ...

  5. C#动态表达式计算

    C#动态表达式计算 应该有不少人开发过程中遇到过这样的需求,我们直接看图说话: 如上图所示,其中Entity为实体类,其中包括五个属性,该五个属性的值分别来自于数据库查询结果: 用户通过可视化界面进行 ...

  6. C#动态表达式计算(续2)

    上两篇废话太多,这一次我就不多说了,由于代码比较简单,可以直接从https://github.com/scottshare/DynamicExpress.git地址下载. 以下说明一下使用方法: Dy ...

  7. 栈应用之 后缀表达式计算 (python 版)

    栈应用之 后缀表达式计算 (python 版) 后缀表达式特别适合计算机处理 1.  中缀表达式.前缀表达式.后缀表达式区别  中缀表达式:(3 - 5) * (6 + 17 * 4) / 3 17 ...

  8. C++实现 逆波兰表达式计算问题

    C++实现 逆波兰表达式计算问题 #include <iostream> #include <string> using namespace std; class Stack ...

  9. 算法笔记_044:表达式计算求值(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的 ...

随机推荐

  1. xtu summer individual 3 F - Opening Portals

    Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  2. 动态规划之最长递增子序列(LIS)

           在一个已知的序列{ a1,a2,……am}中,取出若干数组成新的序列{ ai1, ai2,…… aim},其中下标 i1,i2, ……im保持递增,即新数列中的各个数之间依旧保持原数列中 ...

  3. Python模块:Re模块、附软件开发目录规范

    Re模块:(正则表达式) 正则表达式就是字符串的匹配规则 正则表达式在多数编程语言里都有相应的支持,Python里面对应的模块时re 常用的表达式规则:(都需要记住) “ . ”   #  默认匹配除 ...

  4. js程序基础字符串具体

    1.     .charAt()里面写数字 在一般浏览器上相当于方括号  可是由于IE6的存在他就有了用处  由于IE6不兼容方括号 2.     charCodeAt()    和charAt几乎相 ...

  5. Java之旅hibernate(2)——文件夹结构

    Hibernate的jar最好是到官网上进行下载.下载最新的稳定的版本号.之后进行解压,以下我们介绍一下hibernate的包结构. 1.      包结构 我们能够看到包文件夹结构发生了变化.我以5 ...

  6. 【Swift】学习笔记(二)——基本运算符

    运算符是编程中用得最多的,其包含一元,二元和三元 三种运算符.swift也和其他编程语言一样基本就那些,以下总结一下,也有它特有的运算符.比方区间运算符 1.一元运算符 =   赋值运算符,用得最多的 ...

  7. java中的ShortBuffer

    一.概述 java.lang.Object java.nio.Buffer java.nio.ShortBuffer public abstract class ShortBuffer extends ...

  8. 自己动手写shell命令之ls -R1fF

    ls命令的R參数代表递归的列出全部子目录中的全部文件,1表示每一行仅仅显示一个文件或目录,f表示不排序即输出.F表示在每项的输出的最后依据其文件类型对应的加上*/=>@|字符.通过c语言实现ls ...

  9. java 学习第一步---安装JDK以及配置环境变量

    1.下载jdk 链接:https://pan.baidu.com/s/1FiTGhxdHK0KTFawdkLT26g    提取码:zcy0    我已经在官网上面下载了1.8的jdk,通过百度云盘分 ...

  10. MyEclipse,Eclipse注释规范模板

    注释的作用:myeclipse中java文件头注释格式设置,最终可用于javadoc.exe生成API文档,同时提高了代码的可读性 设置方法: windows->preferences-> ...