题解 P1575 【正误问题】

· · 题解

本人是一个普及组蒟蒻,做这道题用了3天
尴尬……………………
看来我还是太蒻了

注:以上是本人的做题经历

正文

本题用栈来做,我们利用C++STLstack模板来做!

步骤1:划分等级

not>and>or,这个东西大家一定要记住!!!

下面附上部分代码:

if(st=="not")
            num.push(3);
        else
            if (st=="and")
            {
                while (!num.empty()&&(num.top()>=2))
                    chuzhan();
                num.push(2);
            }
            else
                if (st=="or")
                {
                    while (!num.empty())
                        chuzhan();
                    num.push(1);
                }
                else
                    if (st=="true")
                            f.push(1);
                        else
                            if(st=="false")
                                f.push(0);

步骤2:出栈

先把比该运算符小的全部出栈,然后再一起出栈

if (num.top()==3)
    {
        if (f.empty())
        {
            cout<<"error";
            exit(0);
        }
        bool flag=f.top();
        flag=!flag;
        f.pop();
        f.push(flag);
    }
    if (num.top()==2)
    {
        if (f.size()<2)
        {
            cout<<"error";
            exit(0);
        }
        bool flag=f.top();
        f.pop();
        flag=flag and f.top();
        f.pop();
        f.push(flag);
    }
    if (num.top()==1)
    {
        if (f.size()<2)
        {
            cout<<"error";
            exit(0);
        }
        bool flag=f.top();
        f.pop();
        flag=flag or f.top();
        f.pop();
        f.push(flag);
    }
    num.pop();

最后附上代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<string>
#include<stack>
using namespace std;
string st;
stack<int>num;
stack<bool>f;
void chuzhan()
{
    if (num.top()==3)
    {
        if (f.empty())
        {
            cout<<"error";
            exit(0);
        }
        bool flag=f.top();
        flag=!flag;
        f.pop();
        f.push(flag);
    }
    if (num.top()==2)
    {
        if (f.size()<2)
        {
            cout<<"error";
            exit(0);
        }
        bool flag=f.top();
        f.pop();
        flag=flag and f.top();
        f.pop();
        f.push(flag);
    }
    if (num.top()==1)
    {
        if (f.size()<2)
        {
            cout<<"error";
            exit(0);
        }
        bool flag=f.top();
        f.pop();
        flag=flag or f.top();
        f.pop();
        f.push(flag);
    }
    num.pop();
}
int main()
{
    while (cin>>st)
        if(st=="not")
            num.push(3);
        else
            if (st=="and")
            {
                while (!num.empty()&&(num.top()>=2))
                    chuzhan();
                num.push(2);
            }
            else
                if (st=="or")
                {
                    while (!num.empty())
                        chuzhan();
                    num.push(1);
                }
                else
                    if (st=="true")
                            f.push(1);
                        else
                            if(st=="false")
                                f.push(0);
    while (!num.empty())
        chuzhan();
    if (f.size()==1)
        if (f.top())
            cout<<"true";
        else
            cout<<"false";
    else
        cout<<"error";
    return 0;
}