0
何文轩
高级守护
高级守护
麻烦大佬帮我看一下为什么我的代码出问题了
#include<iostream>
#include<stack>
#include<cstdio>
using namespace std;
stack<char> q1;
stack<char> q2;
string s;
int main(){
cin>>s;
int n=s.size();
s=s+' ';
for(int i=1;i<=n;++i){
if(s[i]=='(') q1.push(s[i]);
else if(s[i]=='[') q2.push(s[i]);
if(s[i]==')'){
if(q1.empty()){
cout<<"Wrong";
return 0;
}
else q1.pop();
}
else if(s[i]==']'){
if(q2.empty()){
cout<<"Wrong";
return 0;
}
else q2.pop();
}
}
if(q1.empty() && q2.empty()) cout<<"OK";
else cout<<"Wrong";
return 0;
}
0
0
缪鲲鹏
新手光能
新手光能
您这写的也太复杂了吧 [( ])或([ ]( )或 ( ( ) ) )均为错误的匹配看到这句话了吗.常规思路再加个判断就行了
if(a == ')')
{
if(s.top() == '(')
s.pop();
else {
cout << "Wrong";
return 0;
}
}
if(a == ']')
{
if(s.top() == '[')
s.pop();
else {
cout << "Wrong";
return 0;
}
}
0
0
0
0
0