PRoblem Description 給你一串字符,不超過50個字符,可能包括括號、數字、字母、標點符號、空格,你的任務是檢查這一串字符中的( ) ,[ ],{ }是否匹配。
Input 輸入數據有多組,處理到文件結束。
Output 如果匹配就輸出“yes”,不匹配輸出“no”
Example Input
sin(20+10){[}]Example Output
yesnoHint
Author ma6174
#include <stdio.h>#include<math.h>#include <stack>#include <iostream>#include <algorithm>#include <bits/stdc++.h>using namespace std;int main(){ char k[100]; while(gets(k)) { stack <char> p; int b=strlen(k); int a; for(a=0; a<b; a++) { if(k[a]=='('||k[a]=='['||k[a]=='{') p.push(k[a]); else if(k[a]==')') { if(!p.empty()&&p.top()=='(')p.pop(); else break; } else if(k[a]==']') { if(!p.empty()&&p.top()=='[')p.pop(); else break; } else if(k[a]=='}') { if(!p.empty()&&p.top()=='{')p.pop(); else break; } } if(a==b&&p.empty())printf("yes/n"); else printf("no/n"); } return 0;}新聞熱點
疑難解答