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


  • 相关阅读:
    对Android开发者有益的40条优化建议
    git推送tag到远端服务器
    详细注释!二维码条码扫描源码,使用Zxing core2.3
    探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法
    android的logcat详细用法!
    【Android】开源项目汇总-备用
    android 中theme.xml与style.xml的区别
    Android实现对HOME键的捕获和屏蔽
    java中newInstance()和new()
    java Stack
  • 原文地址:https://www.cnblogs.com/demodemo/p/4678400.html
Copyright © 2011-2022 走看看