返回列表 發帖

[作業繳交] 2025/03/29

本帖最後由 方浩葦 於 2025-3-29 14:58 編輯

1.『基礎題庫』- a045

2. APCS 觀念題 (考古題):10510 - 13、14

TOP

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

本帖最後由 許浩浩 於 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

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

返回列表