Oscar Shiang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 程式設計(二) Lab 3 講解 ###### tags: `lion_1082` ## lab3 A 部分 ### 題目說明 > 根據從 `file.in` 讀入的資料,將最重的 5 隻牛的體重加總起來輸出 ### 使用 [fstream](http://cplusplus.com/reference/fstream/fstream/) 1. 使用前需要在原始碼的上方加上 `#include <fstream>` 2. 利用 `constructor` 進行開檔或是利用 `member function` 的形式開啟檔案 `open` 函式的[參數形式](http://cplusplus.com/reference/fstream/fstream/open/)是 `open(filename, open_mode)` 常見的開啟模式有以下幾種: - `ios::in` : 以讀取模式開啟檔案 - `ios::out` : 以寫入模式開啟檔案 - `ios::app` : 以寫入模式開啟檔案並將寫入指針移動到檔案結尾處 詳細的模式說明可以參考 [ios_base::openmode - C++ Reference](http://cplusplus.com/reference/ios/ios_base/openmode/) ### 如何讀取檔案? 如同使用 `cin` 與 `cout` 一樣 - 如果你是以 `ios::in` 開啟檔案的話,可以把你所宣告的 fstream 變數當作 `cin` 來讀入資料 ```cpp #include <iostream> #include <fstream> using namespace std; int main() { fstream ff; // 宣告 fstream 變數 int b; ff.open('input.txt', ios::in); // 利用 open 函式開啟檔案 ff >> b; // 從 ff 讀入資料至 b 變數 cout << b << endl; ff.close(); // 關閉檔案 return 0; } ``` ### [vector](http://www.cplusplus.com/reference/vector/vector/) 使用 簡單來說, vector 可以說是 array 的進化版 在 C++ 中,有提供許多好用的 [STL](https://www.runoob.com/cplusplus/cpp-stl-tutorial.html) ,而 Vector 就是其中一個 [STL](https://www.runoob.com/cplusplus/cpp-stl-tutorial.html) #### 使用方法 - 須將 `#include <vector>` 加入程式碼中 - 宣告的時候要告訴編譯器這個 Vector 所要儲存的**元素的資料型態** - 使用 `push_back` 將元素加入 Vector - 可以使用 `[]` 或是 `at()` 這個成員函式來存取其中的特定元素 - 使用 `size()` 取得 Vector 的大小 ```cpp #include <iostream> #include <vector> using namespace std; int main() { vector <int> aa; // 在 < > 中告知元素的資料型態 for (int i = 0; i < 10; i++) { aa.push_back(i); // 將變數 i 的數值存進 aa 中 } // now 0 ~ 9 are in the vector named `aa` cout << aa[3] << endl; // output: 3 cout << aa.at(3) << endl; // output: 3 cout << aa.size() << endl; // output: 10 return 0; } ``` ### [sort](http://www.cplusplus.com/reference/algorithm/sort/) 使用 - 要將 `#include <algorithm>` 加進程式碼裡 - `sort` 函式的[參數結構](http://www.cplusplus.com/reference/algorithm/sort/) : `sort(begin_pointer, end_pointer, function_pointer)` - `sort` 可以根據你所提供的 function 來改變排序的方式 - `sort` 可以使用在許多的資料型態上(如 array, vector, string 等等) ```cpp #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector <int> aa; aa.push_back(0); aa.push_back(3); aa.push_back(2); aa.push_back(5); // now aa is [0, 3, 2, 5] sort(aa.begin(), aa.end()); // the result is [0, 2, 3, 5] return 0; } ``` 搭配傳入比較函數可以做不同的變化 ```cpp #include <iostream> #include <algorithm> #include <vector> using namespace std; // 定義比較函式 bool cmp(const int a, const int b) { return a > b; } int main() { vector <int> aa; aa.push_back(0); aa.push_back(3); aa.push_back(2); aa.push_back(5); // 利用比較函式更動排序 sort(aa.begin(), aa.end(), cmp); // now the result is [5, 3, 2, 0] return 0; } ``` ### [範例程式碼 (A 部分)](https://github.com/OscarShiang/pd2_lab3/blob/master/lab3_a.cpp) ## lab3 B 部分 ### 題目說明 > 執行 `Vector and Array` 投影片第 21 與 22 頁的程式碼,並依照結果分析 $O(n^2)$ 與 $O(n\ log\ n)$ 的差異 ### 時間複雜度 根據[維基百科](https://en.wikipedia.org/wiki/Time_complexity)的敘述: > 在電腦科學中,演算法的時間複雜度(Time complexity)是一個函式,它定性描述該演算法的執行時間。 > [color= purple] ### [Insertion sort](https://en.wikipedia.org/wiki/Insertion_sort) 與我們日常生活中排撲克牌的方法類似,時間複雜度為 $O(n^2)$ ![](https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif) ### [sort()](https://en.wikipedia.org/wiki/Sort_(C%2B%2B)) 未有統一的實作方法,依照編譯器的不同可能有不同的實作方式 但在 C++11/14 標準裡有規範其時間複雜度為 $O(n\ log\ n)$ ### 執行結果 利用測量得到的執行時間畫出下圖: ![](https://hackmd.io/_uploads/B1kNB7r4n.png) 可以看到 [insertion sort](https://zh.wikipedia.org/zh-tw/插入排序) 在元素越來越多的時候,其所花費時間的上升曲線會比使用 sort 函式還來得陡,這是因為 [insertion sort](https://zh.wikipedia.org/zh-tw/插入排序) 在時間複雜度上屬於 $O(n^2)$ 而 sort 函式根據 C++11/14 的標準,時間複雜度為 $O(n\ log\ n)$ ,所以兩者在排序數量漸增時所需花費時間的差異就會逐漸顯現出來。 ### [範例程式碼 (B 部分)](https://github.com/OscarShiang/pd2_lab3/blob/master/lab3_b.cpp)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully