4月考试错题总结

T1:P1739 表达式括号匹配

错误代码:

#include<iostream>
#include<stack>
using namespace std;
int main() {
	stack<char>	q;
	string a;
	cin>>a;
	for(int i=0;i<a.size()-1;i++)
	{
		if(a[i]=='(')
			q.push('(');
		else if(a[i]==')')
			if(q.size())
				q.pop();
			else
			{
				cout<<"No";
				return 0;
			}
	}
	if(!q.size())
		cout<<"YES";
	else
		cout<<"NO";
	return 0;
}

代码错误点

else
{
	cout<<"No";
	return 0;
}

修改方案

cout<<"No";//错误代码
cout<<"NO";//更改后代码

正确代码

#include<iostream>
#include<stack>
using namespace std;
int main() {
	stack<char>	q;
	string a;
	cin>>a;
	for(int i=0;i<a.size()-1;i++)
	{
		if(a[i]=='(')
			q.push('(');
		else if(a[i]==')')
			if(q.size())
				q.pop();
			else
			{
				cout<<"NO";
				return 0;
			}
	}
	if(!q.size())
		cout<<"YES";
	else
		cout<<"NO";
	return 0;
}

T2:[ABC328D] Take ABC

错误代码:

#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
int main()
{
    string a;
    cin >> a;
    stack<char> q;
    int len = a.size();
    for (int i = 0; i < len; i++)
    {
        if (a[i] == 'A' || a[i] == 'B')
        {
            q.push(a[i]);
        }
        else if (a[i] == 'C')
        {
            if (q.top() == 'B' && q.size() > 2)
            {
                q.pop();
                if (q.size())
                {
                    if (q.top() == 'A')
                    {
                        q.pop();
                        continue;
                    }
                    else
                    {
                        q.push('B');
                        q.push(a[i]);
                    }
                }
                else
                {
                    q.push('B');
                    q.push(a[i]);
                }
            }
            else
            {
                q.push(a[i]);
            }
        }
    }
    string ans = "";
    while (q.size())
    {
        ans = q.top() + ans;
        q.pop();
    }
    cout << ans;
    return 0;
}

代码错误点

if (q.top() == 'B' && q.size() > 2)

修改方案

if (q.top() == 'B' && q.size() > 2) //错误代码

//正确代码
if (q.size())
	if(q.top() == 'B')
	{
	//中间一样
	}else
   {
   		q.push(a[i]);
   	}
else
{
	q.push(a[i]);
}

正确代码

#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
int main()
{
    string a;
    cin >> a;
    stack<char> q;
    int len = a.length();
    for (int i = 0; i < len; i++)
    {
        if (a[i] == 'A' || a[i] == 'B')
        {
            q.push(a[i]);
        }
        else if (a[i] == 'C')
        {
            if (q.size())
            {
                if (q.top() == 'B')
                {
                    q.pop();
                    if (q.size())
                    {
                        if (q.top() == 'A')
                        {
                            q.pop();
                            continue;
                        }
                        else
                        {
                            q.push('B');
                            q.push(a[i]);
                        }
                    }
                    else
                    {
                        q.push('B');
                        q.push(a[i]);
                    }
                }
                else
                {
                    q.push(a[i]);
                }
            }
            else
            {
                q.push(a[i]);
            }
        }
    }
    string ans = "";
    while (q.size())
    {
        ans = q.top() + ans;
        q.pop();
    }
    cout << ans;
    return 0;
}

别问我为什么要发,问就是老师逼的

posted @ 2024-07-21 11:41  非气盈门  阅读(42)  评论(0)    收藏  举报