#39940: 題目c++解


ljie94610@gmail.com (高宇晨)

學校 : 不指定學校
編號 : 235488
來源 : [1.175.168.87]
最後登入時間 :
2024-04-24 00:51:33
k621. [紅]括號匹配問題 | From: [114.40.71.76] | 發表日期 : 2024-04-14 13:43

#include <iostream>
#include<stack> //使用c++中的stack
using namespace std;
int main() {
char s;
stack<char> ans;
while(cin>>s){
if(s=='(' || s==')'){ //判斷括號種類
if(s=='('){
ans.push('(');
}
else if(s==')'){
if(ans.empty() || ans.top() != '('){ //檢查stack中是否為空的或是stack的頂部不是此括號種類
cout<<"Wrong";
return 0;
}
else ans.pop();
}
}
else if(s=='[' || s==']'){
if(s=='[') ans.push('[');
else if(s==']'){
if(ans.empty() || ans.top() != '['){
cout<<"Wrong";
return 0;
}
else ans.pop();
}
}
}
if(ans.size() == 0 ) cout<<"Right"; //最後檢查stack大小是否為空的
else cout<<"Wrong";
return 0;
}
 
ZeroJudge Forum