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

TOP

返回列表