PTA上的一道括号匹配检测题目,有两个检测点过不去,是有哪些没考虑到的问题吗?(附上题目和代码)
程序代码:#include<cstdio>
#include<cstdlib>
#include<stack>
#include<cstring>
using namespace std;
int main(){
char str[110];
stack<char> stk;
fgets(str, 110, stdin);
str[strcspn(str, "\n")] = 0;
int n = 0;
int i = 0;
while(str[i]!='\0'){
if(str[i]=='(' || str[i]=='[' || str[i]=='{'){ //遇见左括号进栈
stk.push(str[i]);
}
else if(str[i]==')' || str[i]==']' || str[i]=='}'){ //遇见右括号匹配
if(stk.empty()){ //栈为空无法匹配的情况
i++;
continue;
}
else{ //栈不为空,左右对应可匹配的情况
if(str[i]==')' && stk.top()=='('){
stk.pop();
n++;
}
else if(str[i]==']' && stk.top()=='['){
stk.pop();
n++;
}
else if(str[i]=='}' && stk.top()=='{'){
stk.pop();
n++;
}
else{ //栈不为空,左右不对应不可匹配的情况
i++;
continue;
}
}
}
i++;
}
printf("%d\n",n);
}



,原来出现这种情况就可以直接输出0了,那我连题目的意思都没理解清楚,现在明白了,谢谢