UVaLive 6833 Miscalculation (表达式计算)
题意:给定一个表达式,只有+*,然后问你按照法则运算和从左到右计算结果有什么不同。
析:没什么可说的,直接算两次就好。
代码如下:
#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 (表达式计算)的更多相关文章
- UVAlive 6833 Miscalculation 字符串处理
去年省选的题 因为卡了这道题再加上队友占机时 省选第一天华丽爆零了 用事实证明了1+1+1<1的事实 毕竟下半年单挑了东北赛名额 省赛打不出来名额就真的就不怪我了(摔 现在有拿出来做 长个记性 ...
- 模拟/字符串处理 UVALive 6833 Miscalculatio
题目传送门 /* 模拟/字符串处理:主要是对*的处理,先把乘的预处理后再用加法,比如说是:1+2*3+4 = 1+..6+4 = 11 */ #include <cstdio> #incl ...
- .NET平台开源项目速览(8)Expression Evaluator表达式计算组件使用
在文章:这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,给大家初步介绍了一下Expression Evaluator验证组件.那里只是概述了一下,并没有对其使用和强大功能做 ...
- C# - 二叉树表达式计算
很早以前就写过双栈的表达式计算. 这次因为想深入学一下二叉树,网上都是些老掉牙的关于二叉树的基本操作. 感觉如果就学那些概念,没意思也不好记忆.于是动手写了一个表达式计算的应用例子. 这样学习印象才深 ...
- C#动态表达式计算
C#动态表达式计算 应该有不少人开发过程中遇到过这样的需求,我们直接看图说话: 如上图所示,其中Entity为实体类,其中包括五个属性,该五个属性的值分别来自于数据库查询结果: 用户通过可视化界面进行 ...
- C#动态表达式计算(续2)
上两篇废话太多,这一次我就不多说了,由于代码比较简单,可以直接从https://github.com/scottshare/DynamicExpress.git地址下载. 以下说明一下使用方法: Dy ...
- 栈应用之 后缀表达式计算 (python 版)
栈应用之 后缀表达式计算 (python 版) 后缀表达式特别适合计算机处理 1. 中缀表达式.前缀表达式.后缀表达式区别 中缀表达式:(3 - 5) * (6 + 17 * 4) / 3 17 ...
- C++实现 逆波兰表达式计算问题
C++实现 逆波兰表达式计算问题 #include <iostream> #include <string> using namespace std; class Stack ...
- 算法笔记_044:表达式计算求值(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的 ...
随机推荐
- HUAS Summer Contest#4 D题 DP
Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的 ...
- poj 3253 Fence Repair (优先队列,哈弗曼)
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...
- JPA的一些坑
推荐一篇比较好的介绍JPA的文章:使用 Spring Data JPA 简化 JPA 开发 JPA坑1:不支持Limit查询 JPA是不支持Limit分页查询,而我们有时又因为某些原因不想用JPA提供 ...
- android开发里跳过的坑——图片文件上传失败
使用的apache的httpclient的jar包,做的http图片上传,上传时,服务器总返文件格式不对.后来发现,是由于在创建FileBody时,使用了默认的ContentType引起的.所以服务器 ...
- 【NOIP2016】天天爱跑步(树上差分)
题意: 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.?天天爱跑步?是一个养成类游戏,需要 玩家每天按时上线,完成打卡任务.这个游戏的地图可以看作一一棵包含 N个结点 ...
- Linux下汇编语言学习笔记46 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- jsp内置对象之response、out、config、exception、pageContext。
本文是对Jsp内置对象的response.out.config.exception.pageContext知识点的详细总结. response对象 Response内置对象和request内置对象是相 ...
- [bzoj1014][JSOI2008]火星人prefix_非旋转Treap_hash_二分
火星人prefix bzoj-1014 JSOI-2004 题目大意:给定一个字符串,支持三种操作:1.查询:两个后缀之间的$LCP$:2.单点修改:3.插入一个字符. 注释:$1\le n\le 1 ...
- Memcached与Spring集成的方式(待实践)
主要是基于这几种方式http://www.cnblogs.com/EasonJim/p/7624822.html去实现与Spring集成,而个人建议使用Xmemcached去集成好一些,因为现在官方还 ...
- Google Chrome Developer Tools
原文:https://www.oschina.net/p/chromedevtools Google发布了Google Chrome Developer Tools,这是一系列面向Chrome开发者的 ...