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

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

代码如下:

#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. gcc 编译多个源文件

    序 Linux 内核和许多其他自由软件以及开放源码应用程序都是用 C 语言编写并使用 GCC 编译的. 编译C++程序 编译.链接命令 -c 只编译不里链接 -o链接 例: g++ file1 -c ...

  2. UVa 548 树(已知其中两种遍历, 还原树)

    题意: 给出后序遍历和先序遍历, 还原一棵树, 然后求出从根节点到叶子的最小路劲和. 分析: 已知后序遍历, 那么后序的最后一个节点就是根节点, 然后在中序中找到这个节点, 它的左边就是左子树, 它的 ...

  3. UVA 1589 象棋

    题意: 给出一个黑方的将, 然后 红方有 2 ~ 7 个棋子, 给出摆放位置,问是否已经把黑将将死, 红方已经将军. 分析: 分情况处理, 车 马 炮, 红将情况跟车是一样的. 建一个数组board保 ...

  4. 添物不花钱学JavaEE(基础篇) --HTML

    HTML是什么? HTML – Hyper Text Markup Language HTML官方网址 http://www.w3.org/TR/2014/REC-html5-20141028/ 其实 ...

  5. zoj4710暴力

    #include<stdio.h> #include<string.h> #define N 110 int map[N][N]; int main() { int n,m,k ...

  6. DEA中MAVEN项目有多个子目录,如何加载构建

    ddts这个项目有三个子目录,每个子目录下面也都有一个 pom.xml       此时需要 右键子目录的 pom.xml,选择Add as Maven Project,在上图中cli.core两个目 ...

  7. JAVA调用动态链接库(dll)

        菜鸡爬坑 基础知识  因为某个东西的keygen我只会在win下生成!! 所以只能出此下策!!之前一直是android下用jni调用so文件,现在试下java在win平台下调用dll 首先还是 ...

  8. 命令行man的帮助手册

    http://blog.csdn.net/gatieme/article/details/51656707 指定使用那种语音的手册,可以使用命令选项-M man -M /usr/share/man/z ...

  9. 合并链表 —— 剑指Offer

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则.     思路: 也在考虑能不能更优雅的代码写法,但是最后,还是两边都写一下,能增加代码清晰度. ...

  10. 几种常见排序算法的java实现

    一.几种常见的排序算法性能比較 排序算法 最好时间 平均时间 最坏时间 辅助内存 稳定性 备注 简单选择排序 O(n^2) O(n^2) O(n^2) O(1) 不稳定 n小时较好 直接插入排序 O( ...