Lazy Math Instructor
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 3721 | Accepted: 1290 |
Description
You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.
Input
- Single letter variables (case insensitive).
- Single digit numbers.
- Matched left and right parentheses.
- Binary operators +, - and * which are used for addition, subtraction and multiplication respectively.
- Arbitrary number of blank or tab characters between above tokens.
Note: Expressions are syntactically correct and evaluated from left to right with equal precedence (priority) for all operators. The coefficients and exponents of the variables are guaranteed to fit in 16-bit integers.
Output
Sample Input
3
(a+b-c)*2
(a+a)+(b*2)-(3*c)+c
a*2-(a+c)+((a+c+e)*2)
3*a+c+(2*e)
(a-b)*(a-b)
(a*a)-(2*a*b)-(b*b)
Sample Output
YES
YES
NO
题目意思:输入两行公式,判断这两行公式相不相等,如果相等,输出YES,否则输出NO 解题思路:先将方式变成后缀式,后缀式通过栈实现。(不晓得后缀式是什么,就百度后缀式吧,我也是百度的(⊙﹏⊙)b)
变成后缀式之后,再通过栈计算他们的值,这里需要将字母转为ASCII码的值计算。最后判断.......
#include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <stack>
using namespace std;
const int maxn = ;
int priority(char c)
{
if(c=='(')
return ;
else if(c=='*')
return ;
else
return ;
}
void convert(char *str,char *temp)
{
int len = strlen(str),t = ;
char c;
stack<char> st;
for(int i=;i<len;i++)
{
if(str[i]!=' ')
{
c = str[i];
if((c<='z'&&c>='a')||(c>=''&&c<=''))
temp[t++]=c;
else
{
if(st.empty()||c=='(')
st.push(c);
else if(c==')')
{
while(!st.empty()&&st.top()!='(')
{
//push_seq(pn[i],top_seq(p[i]));
temp[t++]=st.top();
st.pop();
}
st.pop();
}
else
{
while(!st.empty()&&priority(c)<=priority(st.top()))
{
temp[t++]=st.top();
st.pop();
}
st.push(c);
}
}
}
}
while(!st.empty())
{
temp[t++]=st.top();
st.pop();
}
temp[t]=;
}
int calculate(char *temp)
{
int len = strlen(temp),x,y,z;
char c;
stack<int> st;
for(int i=;i<len;i++)
{
c=temp[i];
if(c>=''&&c<='')
st.push(c-'');
else if(c<='z'&&c>='a')
st.push(int(c));
else
{
x=st.top();
st.pop();
y=st.top();
st.pop();
switch(c)
{
case '*': z = x*y; break;
case '+': z = x+y; break;
case '-': z = y-x; break;
}
st.push(z);
}
}
return st.top();
}
int main()
{
freopen("in.txt","r",stdin);
char str[maxn],temp[maxn];
int n;
scanf("%d",&n);
getchar();//此处不能忘记getchar(),否则会出错
while(n--)
{
gets(str);
convert(str,temp);
int ans1=calculate(temp);
gets(str);
convert(str,temp);
int ans2=calculate(temp);
if(ans1==ans2)
printf("YES\n");
else
printf("NO\n");
}
}
Lazy Math Instructor的更多相关文章
- 数据结构——POJ 1686 Lazy Math Instructor 栈的应用
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...
- poj 1684 Lazy Math Instructor(字符串)
题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...
- UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)
因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号) sum += Cal( ...
- POJ 1686 Lazy Math Instructor(栈)
原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
- lazy instructor
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- Problem K 栈
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- PQJ 1686(栈栈栈)
PQJ 1686(栈栈栈) 用栈解决问题 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
随机推荐
- Python之路Day7
第7天主要是面向对象的内容. 学到现在越来越吃力了,从上节课开始博客就没时间写了,看看别人写的博客都那么棒.又想起了那句话比你牛逼的人都在努力,你却在放羊...唉,我写作业的效率有点低,以后得抓紧时间 ...
- 基于Sql Server 2008的分布式数据库的实践(二)
原文 基于Sql Server 2008的分布式数据库的实践(二) 从Win7连接Win2003的Sql Server 2008 1.新建链接服务器链接到Win2003的Sql Server 2008 ...
- Cocos2d-x 3.0final手机游戏开发视频教程2014 - 自学编程 -(陆续更新中)
内容: 非常多人问我:沈老师,要不要更新引擎版本号到3.0,更新这么快,以后会不会每一个月都有一次,好怕呀. 我说:无论你曾经是哪个版本号,3.0final是一个架构级别的升级,能够在新项目中果断升级 ...
- jQuery 快速结束当前动画
当需要快速结束一个当前正在执行的jquery 动画时(还没执行完成),如 fadeOut(),可以在执行当前动画的对象上执行 stop(true);方法 如: <script type=&quo ...
- SQL Server 基础 03 查询数据基础
查询数据 简单的查询 create table stu_info ( sno int not null ,sname ) not null ,sex ) not null ,birth ) not n ...
- SuperSocket源码解析之开篇
一 简介 官方介绍:SuperSocket 是一个轻量级, 跨平台而且可扩展的 .Net/Mono Socket 服务器程序框架.你无须了解如何使用 Socket, 如何维护 Socket 连接和 S ...
- android页面切换效果
两种方式: 在activity的自定义主题中定义切换方式: overridePendingTransition()方法 自定义主题: 在项目的res/values/styles.xml中添加样式 &l ...
- jquery validate 插件使用小结
项目中整合了jquery validate插件,仿照别人的使用写了我的表单验证,结果不起作用.然后就各种找原因. 在网上下了jquery validate插件的完整包,看了看里边的例子,跟我的使用貌似 ...
- AdTime:多屏时代下传统媒体的鼓起
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzk1MTQzNQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- github上的QT源码,必要的时候还是应该看一下,仅凭猜测很容易出错
QCoreApplication::processEvents 他处理的时候拿的是current不是qAppqApp的话,才是和主线程密切相关的 一直觉得QT源码复杂,有点怕,所以没怎么看 我也看不懂 ...