4.13日测试总结
4.13日测试总结
T1: P1427 小鱼的数字游戏
运用栈的特点先进后出(LIFO)的特点 其实可以用数组写
错误代码:
出现RE:
#include<bits/stdc++.h>
using namespace std;
stack<int> op;
int main()
{
for(int i=1;;i++)
{
int x;
cin>>x;
op.push(x);
if(x==0)
{
break;
}
}
int len=op.size();
op.pop();
for(int i=1;i<=len;++i)
{
cout<<op.top()<<" ";
op.pop();
}
return 0;
}
错在了:
int len=op.size();
op.pop();
for(int i=1;i<=len;++i)
{
cout<<op.top()<<" ";
op.pop();
}
因为是要先删除再计算长度,正确的应该是:
op.pop();
int len=op.size();
for(int i=1;i<=len;++i)
{
cout<<op.top()<<" ";
op.pop();
}
AC代码:
#include<bits/stdc++.h>
using namespace std;
stack<int> op;
int main()
{
for(int i=1;;i++)
{
int x;
cin>>x;
op.push(x);
if(x==0)
{
break;
}
}
int len=op.size()-1;
op.pop();
for(int i=1;i<=len;++i)
{
cout<<op.top()<<" ";
op.pop();
}
return 0;
}
T2: P1739 表达式括号匹配
无错误,AC代码:
#include<bits/stdc++.h>
using namespace std;
stack<char> op;
int main()
{
for(int i=1;;++i)
{
char x;
cin>>x;
if(x=='@')
{
break;
}
if(x=='(')
{
op.push('(');
}
if(x==')')
{
if(op.size()==0)
{
cout<<"NO";
return 0;
}
else
{
op.pop();
}
}
}
if(op.size()!=0)
{
cout<<"NO";
}
else
{
cout<<"YES";
}
return 0;
}
T3: B3614 【模板】栈
无错误,AC代码:
#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;
stack<ull> s;
int main(){
ull T;
cin >> T;
for (int i = 0; i < T; i++) {
ull n;
cin >> n;
while (s.empty() != 1)
{
s.pop();
}
for (int j = 0; j < n; j++)
{
string op;
cin >> op;
if (op == "push")
{
ull x;
cin >> x;
s.push(x);
}
else if (op == "pop")
{
if (s.empty() != 1)
{
s.pop();
}
else
{
cout << "Empty" << endl;
}
}
else if (op == "query")
{
if (s.empty() != 1)
{
cout << s.top() << endl;
}
else
{
cout << "Anguei!" << endl;
}
}
else if (op == "size")
{
cout << s.size() << endl;
}
}
}
return 0;
}
T4: U419333 大小组合
老师出的题目到现在还没有做出来
错误代码(改了以后的):
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
stack<char> op;
string s;
cin >> s;
int len = s.size();
for(int i=0;i<len;++i)
{
if (op.empty())
{
op.push(s[i]);
}
else
{
if(s[i] >= 'a' && s[i] <= 'z')
{
if(op.top() == s[i] + 'A' - 'a')
{
op.pop();
}
else
{
op.push(s[i] - 'a' + 'A');
}
}
else
{
if(op.top() == s[i])
{
op.pop();
}
else
{
op.push(s[i]);
if(op.top() == s[i])
{
op.pop();
}
else
{
op.push(s[i]);
}
}
}
}
}
string ans = "";
while (op.size())
{
ans = op.top() + ans;
op.pop();
}
cout << ans << endl;
}
return 0;
}
依然没有做出来
T5: [ABC328D] Take ABC
没提交,因为没有做出来
正确代码:
#include<bits/stdc++.h>
using namespace std;
stack<char>op;
int main()
{
string s;
cin>>s;
int len=s.size();
for(int i=0;i<len;++i)
{
if(s[i]=='A')
{
op.push('A');
}
if(s[i]=='B')
{
op.push('B');
}
if(s[i]=='C')
{
if(op.size()>=2)
{
char B=op.top();
op.pop();
char A=op.top();
op.pop();
if(B=='B' and A=='A')
{
continue;
}
else
{
op.push(A);
op.push(B);
op.push('C');
}
}
else
{
op.push(s[i]);
}
}
}
string ans="";
while(op.size())
{
ans = op.top()+ans;
op.pop();
}
cout<<ans<<endl;
return 0;
}
T6: P1981 [NOIP2013 普及组] 表达式求值
在网上查了一下这叫:
逆波兰表达式(Reverse Polish Notation,简称RPN),也被称为后缀表达式,是一种不需要括号来表示运算符优先级的数学表达式。在这种表达式中,运算符位于其所有操作数之后。这种表达式的优点是它们的计算可以只通过栈(后进先出的数据结构)来完成,简化了计算过程。
逆波兰表达式的特点
- 运算符后置:在逆波兰表达式中,运算符总是跟在其操作数之后。例如,加法运算在传统表达式中表示为
a + b,在逆波兰表达式中则是a b +。- 无需括号:由于运算符的顺序已经明确,因此不需要使用括号来指示优先级。
- 易于计算:使用栈结构可以方便地计算表达式的值。遇到操作数时将其压入栈中,遇到运算符时从栈顶弹出相应数量的操作数,进行计算后将结果压回栈中。
- 适合计算机处理:计算机不需要进行括号匹配和优先级判断,只需要按照表达式顺序进行运算即可。
AC代码:
#include<bits/stdc++.h>
using namespace std;
stack<int> op;
int main()
{
int x;
cin>>x;
x=x%10000;
op.push(x);
char c;
while (cin>>c>>x)
{
if(c=='*')
{
int y = op.top();
op.pop();
op.push(x*y%10000);
}
else
{
op.push(x);
}
}
int sum = 0;
while (op.size())
{
sum += op.top();
sum %= 10000;
op.pop();
}
cout<<sum<<endl;
return 0;
}
本文来自博客园,作者:yucheng0630,转载请注明原文链接:https://www.cnblogs.com/yucheng0630/p/18320696

浙公网安备 33010602011771号