zoukankan      html  css  js  c++  java
  • 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
    ");
    108         else
    109             printf("NO
    ");
    110     }
    111 }
    View Code


  • 相关阅读:
    MySQL学习-- UNION与UNION ALL
    图解MySQL 内连接、外连接、左连接、右连接、全连接……太多了
    mysql的三种连接方式
    Spring Boot MyBatis配置多种数据库
    @Value注解分类解析
    SpringBoot启动报错Failed to determine a suitable driver class
    idea启动报错:Access denied for user 'root '@'192.168.100.XXX' (using password: YES)
    QStandardItemModel的data线程安全(在插入数据时,临时禁止sizeHint去读model中的data)
    ubuntu 交叉编译qt 5.7 程序到 arm 开发板
    继承QWidget的派生类控件不能设置QSS问题解决(使用style()->drawPrimitive(QStyle::PE_Widget,也就是画一个最简单最原始的QWidget,不要牵扯其它这么多东西)
  • 原文地址:https://www.cnblogs.com/demodemo/p/4678400.html
Copyright © 2011-2022 走看看