返回列表 發帖
1.『基礎題庫』- a045
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. bool B(string c)
  4. {
  5.     stack<char> a;
  6.     for(int i=0;i<c.length();i++)
  7.     {
  8.         if(c[i]=='{' or c[i]=='[' or c[i]=='(')
  9.         {
  10.             a.push(c[i]);
  11.         }
  12.         else if(c[i]=='}' or c[i]==']' or c[i]==')')
  13.         {
  14.             if(a.empty() or (c[i]=='}' and a.top() != '{') or
  15.                (c[i] == ']' and a.top() != '[') or
  16.                (c[i]==')' and a.top() != '('))
  17.             {
  18.               return false;
  19.             }
  20.             a.pop();
  21.         }
  22.     }
  23.     return a.empty();
  24. }
  25. int main()
  26. {
  27.     string line;
  28.     int count=0;
  29.     while(getline(cin,line) and count<20)
  30.     {
  31.         if(line.empty())
  32.         {
  33.             break;
  34.         }
  35.         if(line.length()>150)
  36.         {
  37.             continue;
  38.         }
  39.         cout<<(B(line) ? "yes":"no")<<'\n';
  40.         count++;
  41.     }

  42.     return 0;
  43. }
複製代碼
2. APCS 觀念題 (考古題):10510 - 13、14
C
A

TOP

返回列表