返回列表 發帖
本帖最後由 許浩浩 於 2025-4-12 14:16 編輯

1.『基礎題庫』- a045
  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. bool n(string c)
  4. {
  5.     stack<char> s;
  6.     for(int i=0;i<c.length();i++)
  7.     {
  8.         if(c[i]=='(' or c[i]=='[' or c[i]=='{')
  9.             s.push(c[i]);
  10.         else if(s.empty()){
  11.             return false;
  12.         }
  13.         else if((s.top()=='(' and c[i]==')')or(s.top()=='[' and c[i]==']')or(s.top()=='{' and c[i]=='}'))
  14.             s.pop();
  15.     }
  16.     return s.empty();
  17. }

  18. int main()
  19. {
  20.     cin.tie(0);
  21.     cin.sync_with_stdio(0);

  22.     string line;
  23.     int count=0;
  24.     while(cin>>line and count<20)
  25.     {
  26.         if(line.empty())
  27.             break;
  28.         if(line.length()>150)
  29.             continue;
  30.         if(n(line))
  31.             cout<<"yes"<<endl;
  32.         else
  33.             cout<<"no"<<endl;
  34.         count++;
  35.     }

  36.     return 0;
  37. }
複製代碼
2. APCS 觀念題 (考古題):10510 - 13、14
  1. (B)(A)
複製代碼

TOP

返回列表