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

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

代码如下:

#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. 洛谷 3953 NOIP2017提高组Day1 T3 逛公园

    [题解] 先建反向图,用dijkstra跑出每个点到n的最短距离dis[i] 设f[u][k]表示dis(u,n)<=mindis(u,n)+k的方案数.对于边e(u,v,w),走了这条边的话需 ...

  2. Rightmost Digit (求n^n最后一位)

    Description Given a positive integer N, you should output the most right digit of N^N.    Input The ...

  3. html的href标签不能下载apk文件

    解决方案: 打开Internet 服务管理器Internet 服务管理器 网站属性 HTTP头(MIME类型) 新建 扩展名:.apk 类型(MIME): application/vnd.androi ...

  4. hdu 1075 字典树

    #include<stdio.h> #include<iostream> struct node { int num,i; node *a[27]; char s[20];// ...

  5. 特种部队(codevs 1427)

    题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一路士兵已经在正面牵制住了敌人,第二路士兵正在悄悄地从后方秘密潜入敌人的仓库.当他们到达仓库时候,发现 ...

  6. 移动端click事件延迟300ms该如何解决

    window.addEventListener( "load", function() {     FastClick.attach( document.body ); }, fa ...

  7. Mayor's posters POJ - 2528

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  8. [bzoj2443][Usaco2011 Open]奇数度数_树形dp_生成树_并查集

    奇数度数 bzoj-2443 Usaco-2011 Open 题目大意:给定一个n个点m条便有向图,问是否有一种选出一些边的方式使得所有点的度数都是奇数. 注释:$1\le n \le 5\cdot ...

  9. 深入理解hadoop(一)

    hadoop 前世今生  hadoop最早起源于开源收缩引擎nutch,由dong cutting 贡献,但由于nutch最初的设计不能解决数10亿级别的文件存储和索引而遇到了严重的可扩展性问题,直到 ...

  10. 淘宝后台添加颜色尺码动态sku

    废话不多说,直接上代码,用了vue,可直接copy运行 <!DOCTYPE html> <html lang="en"> <head> < ...