b923 stack 堆疊的模板題

題目原文

題目說明
實作 stack 三種功能:1. 刪除堆頂元素 2. 輸出頂端元素  3. 丟數字進堆疊。

思路
利用 STL 裡的堆疊分別使用 pop、top、push 三種 function。

#include<iostream>
#include<stack> 
using namespace std;

int main()
{
 stack<int> s;
 
 int n;
 cin >> n;
 while(n--)
 {
  int select;
  cin >> select;
  
  if(select == 1)
   s.pop();
  if(select == 2)
   cout << s.top() << endl; 
  if(select == 3)
  {
   int num;
   cin >> num;
   s.push(num);
  }  
 }
}

留言

這個網誌中的熱門文章

離散數學筆記 — vertex cut

機率筆記 (1)

機率筆記 (4) — 隨機變數