Lazy Math Instructor

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3721   Accepted: 1290

Description

A math instructor is too lazy to grade a question in the exam papers in which students are supposed to produce a complicated formula for the question asked. Students may write correct answers in different forms which makes grading very hard. So, the instructor needs help from computer programmers and you can help.

You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.

Input

The first line of the input contains an integer N (1 <= N <= 20) that is the number of test cases. Following the first line, there are two lines for each test case. A test case consists of two arithmetic expressions, each on a separate line with at most 80 characters. There is no blank line in the input. An expression contains one or more of the following:

  • 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

Your program must produce one line for each test case. If input expressions for each test data are arithmetically equivalent, "YES", otherwise "NO" must be printed as the output of the program. Output should be all in upper-case characters.

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码的值计算。最后判断.......

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <fstream>
  5 #include <stack>
  6 using namespace std;
  7 const int maxn = 90;
  8 int priority(char c)
  9 {
 10     if(c==‘(‘)
 11         return 0;
 12     else if(c==‘*‘)
 13         return 2;
 14     else
 15         return 1;
 16 }
 17 void convert(char *str,char *temp)
 18 {
 19     int len = strlen(str),t = 0;
 20     char c;
 21     stack<char> st;
 22     for(int i=0;i<len;i++)
 23     {
 24         if(str[i]!=‘ ‘)
 25         {
 26             c = str[i];
 27             if((c<=‘z‘&&c>=‘a‘)||(c>=‘0‘&&c<=‘9‘))
 28                 temp[t++]=c;
 29             else
 30             {
 31                 if(st.empty()||c==‘(‘)
 32                     st.push(c);
 33                 else if(c==‘)‘)
 34                 {
 35                     while(!st.empty()&&st.top()!=‘(‘)
 36                     {
 37                         //push_seq(pn[i],top_seq(p[i]));
 38                         temp[t++]=st.top();
 39                         st.pop();
 40                     }
 41                     st.pop();
 42                 }
 43                 else
 44                 {
 45                     while(!st.empty()&&priority(c)<=priority(st.top()))
 46                     {
 47                         temp[t++]=st.top();
 48                         st.pop();
 49                     }
 50                     st.push(c);
 51                 }
 52             }
 53         }
 54     }
 55     while(!st.empty())
 56     {
 57         temp[t++]=st.top();
 58         st.pop();
 59     }
 60     temp[t]=0;
 61 }
 62 int calculate(char *temp)
 63 {
 64     int len = strlen(temp),x,y,z;
 65     char c;
 66     stack<int> st;
 67     for(int i=0;i<len;i++)
 68     {
 69         c=temp[i];
 70         if(c>=‘0‘&&c<=‘9‘)
 71             st.push(c-‘0‘);
 72         else if(c<=‘z‘&&c>=‘a‘)
 73             st.push(int(c));
 74         else
 75         {
 76             x=st.top();
 77             st.pop();
 78             y=st.top();
 79             st.pop();
 80             switch(c)
 81             {
 82                 case ‘*‘:  z = x*y; break;
 83                 case ‘+‘:  z = x+y; break;
 84                 case ‘-‘:  z = y-x; break;
 85             }
 86             st.push(z);
 87         }
 88     }
 89     return st.top();
 90 }
 91 int main()
 92 {
 93     freopen("in.txt","r",stdin);
 94     char str[maxn],temp[maxn];
 95     int n;
 96     scanf("%d",&n);
 97     getchar();//此处不能忘记getchar(),否则会出错
 98     while(n--)
 99     {
100         gets(str);
101         convert(str,temp);
102         int ans1=calculate(temp);
103         gets(str);
104         convert(str,temp);
105         int ans2=calculate(temp);
106         if(ans1==ans2)
107             printf("YES\n");
108         else
109             printf("NO\n");
110     }
111 }

				
时间: 2024-10-10 19:36:29

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 are supposed to produce a complicated formula for the question asked. Students may write correct answers in different forms which makes grading very ha

UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)

因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号)  sum += Cal(); else sum += num; return sum; } 但是这个题目着实坑了我一下,见过WA了,没见过TLE呢……我因为没有看到有空格这个条件,无线TLE,又是消除函数又是改用数组模拟栈,其实就是读入出错和忘记了处理空格,改了之后,成功AC了.代码如下: #include<iostrea

poj 1684 Lazy Math Instructor(字符串)

题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> using namespace

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

Problem K 栈

Description A math instructor is too lazy to grade a question in the exam papers in which students are supposed to produce a complicated formula for the question asked. Students may write correct answers in different forms which makes grading very ha

PQJ 1686(栈栈栈)

PQJ  1686 用栈解决问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description A math instructor is too lazy to grade a question in the exam papers in which students are supposed to produce a complicated formula for the que

【原创】开源Math.NET基础数学类库使用(六)数值分析之线性方程直接求解

开源Math.NET基础数学类库使用系列文章总目录: 1.开源.NET基础数学计算组件Math.NET(一)综合介绍  2.开源.NET基础数学计算组件Math.NET(二)矩阵向量计算  3.开源.NET基础数学计算组件Math.NET(三)C#解析Matlab的mat格式 4.开源.NET基础数学类库使用Math.NET(四)C#解析Matrix Marke数据格式 5.开源.NET基础数学类库使用Math.NET(五)C#解析Delimited Formats数据格式 6.开源.NET基础

开源Math.NET基础数学类库使用(06)数值分析之线性方程组直接求解

原文:[原创]开源Math.NET基础数学类库使用(06)数值分析之线性方程组直接求解 开源Math.NET基础数学类库使用系列文章总目录:   1.开源.NET基础数学计算组件Math.NET(一)综合介绍    2.开源.NET基础数学计算组件Math.NET(二)矩阵向量计算    3.开源.NET基础数学计算组件Math.NET(三)C#解析Matlab的mat格式   4.开源.NET基础数学类库使用Math.NET(四)C#解析Matrix Marke数据格式   5.开源.NET基

LightOj 1148 Basic Math

1148 - Mad Counting PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive approach to this pro