本帖最後由 許浩浩 於 2025-4-12 14:16 編輯
1.『基礎題庫』- a045- #include<bits/stdc++.h>
- using namespace std;
- bool n(string c)
- {
- stack<char> s;
- for(int i=0;i<c.length();i++)
- {
- if(c[i]=='(' or c[i]=='[' or c[i]=='{')
- s.push(c[i]);
- else if(s.empty()){
- return false;
- }
- else if((s.top()=='(' and c[i]==')')or(s.top()=='[' and c[i]==']')or(s.top()=='{' and c[i]=='}'))
- s.pop();
- }
- return s.empty();
- }
- int main()
- {
- cin.tie(0);
- cin.sync_with_stdio(0);
- string line;
- int count=0;
- while(cin>>line and count<20)
- {
- if(line.empty())
- break;
- if(line.length()>150)
- continue;
- if(n(line))
- cout<<"yes"<<endl;
- else
- cout<<"no"<<endl;
- count++;
- }
- return 0;
- }
複製代碼 2. APCS 觀念題 (考古題):10510 - 13、14 |