1.『基礎題庫』- a045- #include<bits/stdc++.h>
- using namespace std;
- bool B(string c)
- {
- stack<char> a;
- for(int i=0;i<c.length();i++)
- {
- if(c[i]=='{' or c[i]=='[' or c[i]=='(')
- {
- a.push(c[i]);
- }
- else if(c[i]=='}' or c[i]==']' or c[i]==')')
- {
- if(a.empty() or (c[i]=='}' and a.top() != '{') or
- (c[i] == ']' and a.top() != '[') or
- (c[i]==')' and a.top() != '('))
- {
- return false;
- }
- a.pop();
- }
- }
- return a.empty();
- }
- int main()
- {
- string line;
- int count=0;
- while(getline(cin,line) and count<20)
- {
- if(line.empty())
- {
- break;
- }
- if(line.length()>150)
- {
- continue;
- }
- cout<<(B(line) ? "yes":"no")<<'\n';
- count++;
- }
- return 0;
- }
複製代碼 2. APCS 觀念題 (考古題):10510 - 13、14
C
A |