zoukankan      html  css  js  c++  java
  • 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 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

    题意:

    给出两个数学式子判断其结果是否相等。
    解法:
    1,用栈将表达式转换成为后缀式,然后计算后缀表达式的只判断其是否相等。
    2,字母转换之后算其值来代表其字母的值,直接将其ASCII作为数值对待,这个题只是判断两个表达式是否在数值上是等价的而不是判断两个公式是否等价,一直很疑惑,查了一下发现比如说:(b-a+c)*2 与 (1+c)*2也相等,但是如果作为公式的话这两个是不相等的.

    3.从程序看出,要先判断字符(juge),然后依次输入(in),然后计算出(put),最后输出(out)结果。

    4.注意格式,空格或tab!

    经过一番搜寻加完善的AC代码:

    #include<iostream>
    #include<fstream>
    #include<stack>
    #include<cstring>
    #include<map>
    using namespace std;
    char c1[500],c2[500];
    char s1[500],s2[500],s[500];
    int a[100];
    int juge(char c)                        //判断
    {
        if(c>='0'&&c<='9') return 1;
        if(c>='a'&&c<='z') return 1;
        else return 0;
    }
    void in(char c[])                       //输入
    {
        int i,j=0;
        stack<char> q;
        for(i=0;i<strlen(c);i++)
        {
             if(juge(c[i]))
                s[j++]=c[i];
             else if(c[i]=='(')
                    q.push(c[i]);
             else if(c[i]==')')
                    {
                        while(q.top()!='(')
                        {
                            s[j++]=q.top();
                            q.pop();
                        }
                        q.pop();
                    }
                    else
                        if(c[i]=='+'||c[i]=='-'||c[i]=='*')
                        {
                            while(!q.empty()&&a[c[i]]<=a[q.top()])
                            {
                                s[j++]=q.top();
                                q.pop();
                            }
                            q.push(c[i]);
                        }
        }
        while(!q.empty())
        {
            s[j++]=q.top();
            q.pop();
        }
        s[j]='';                               //特别注意,很容易遗漏
    }
    
    int put(char s1[])                           //计算出值
    {
        int i,j,k;
        stack<int> q;
        for(i=0;i<strlen(s1);i++)
        {
            if(juge(s1[i]))
            {
                if(s1[i]>='0'&&s1[i]<='9')
                    q.push(s1[i]-'0');
                else
                    q.push(s1[i]);
            }
            else
            {
                j=q.top();
                q.pop();
                k=q.top();
                q.pop();
                if(s1[i]=='+')
                    j=j+k;
                if(s1[i]=='-')
                    j=k-j;
                if(s1[i]=='*')
                    j=k*j;
                q.push(j);
            }
        }
        return q.top();
    
    }
    void out()                                      //输出比较
    {
        a['+']=1;
        a['-']=1;
        a['*']=2;
        a['(']=0;
        int i,j,t;
        cin>>t;
        getchar();
        while(t--){
            cin.getline(c1,500);
            cin.getline(c2,500);
            in(c1);
            strcpy(s1,s);
            in(c2);
            strcpy(s2,s);
            i=put(s1);
            j=put(s2);
            if(i==j) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    
    int main()
    {
        out();
        return 0;
    }
  • 相关阅读:
    关于 控制反转与依赖注入 对初学者的一点帮助
    转载--文章(感谢米粒儿博主分享) 关于 Json.net序列化时间问题
    转载--文章(感谢陈晨博主分享) 关于 Json.net
    B-JUI框架使用探究
    什么是Entity Framework(ORM)
    字典
    Docker部署Linux+Nginx+Mariadb+PHP环境
    Docker部署Django+nginx+uwsgi环境
    Docker部署Django+apache+mod_wsgi环境
    Docker容器技术
  • 原文地址:https://www.cnblogs.com/hfc-xx/p/4663558.html
Copyright © 2011-2022 走看看