HOVOH
    • 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
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • 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

      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.
      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
    • Engagement control
    • 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
Engagement control 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
Engagement control Commenting, Suggest edit, Emoji Reply
  • 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

    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.
    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
    --- type : slide --- 競程初探 === by HOVOH&Wenyt --- ## 什麼是程式 ---- 人類用來跟電腦溝通的語言 --- ## 要在哪裡寫程式? ---- 打字要用word 做簡報要用powerpoint 那打程式呢? ---- IDE整合開發環境 ![](https://assets.codegrip.tech/wp-content/uploads/2022/03/11154314/Blog-Feature-image-1200x628-1.png) ---- * [DEV C++](https://www.bloodshed.net/) * [codeblock](https://www.codeblocks.org/downloads/) ![](https://i.imgur.com/9DYIRXC.png) ![](https://miro.medium.com/v2/resize:fit:659/1*9gPFBc7OVbJRoeW_oyEC5A.png) --- ## C++ ---- 基本上競程選手幾乎使用 C++,較不會使用 C、Python、Java、Go 等語言 有幾點原因: 1. C++ 很快 2. C++ 相對 C 有很多寫好的資料結構可以直接用 3. C++ 在幾乎所有比賽中都能使用 4. C++ 對於操作陣列、指標的難度最低 但若有其他語言更為適合的情況,也可以試試 --- ## 名詞介紹 ---- * 電神:電爛你的人 * 水題:簡單題 * 燒雞:裂開,可能程式爛掉 * 裝弱:電神最愛做的事 --- ## 解題狀況 ---- <font color="green">AC(Accepted)</font>:通過 <font color="red">WA(Wrong Answer)</font>:答案錯誤 <font color="blue">CE(Conpile Error)</font>:編譯錯誤(先在IDE上編譯) <font color="yellow">RE(Runtime Error)</font>:執行時錯誤(陣列越界、除以0...) <font color="grey">TLE(Time Limit Exceeded)</font>:執行超過時間限制 <font color="brown">MLE(Memory Limit Exceeded)</font>:程序執行超過記憶體限制 --- ## Judge網站 ---- 就是大家刷題的地方 ---- 首先是最重要的 ---- **[MDjudge](http:// ``` * [zerojudge](https://zerojudge.tw/):有很多題目,題目難度浮動很大,你解到一定程度也可以出題,俗稱"水題庫" * [TIOJ](https://tioj.ck.tp.edu.tw/):建中的judge,有很多很難的題目 * [tcirc](https://judge.tcirc.tw/):一中的judge,上面有AP325的完整題庫 * [codeforces](https://codeforces.com/):每週都有比賽,有積分制,題目只有俄文或是英文(看到紅黑人記得膜拜) * [ATcoder](https://atcoder.jp/):每週都有比賽,不過是日本的網站,題目只有日文或是英文 * [CSES](https://cses.fi/problemset/):芬蘭的網站,題目只有芬蘭文或是英文,可以順便練英文能力 * [Leetcode](https://leetcode.com/):許多工程師面試前刷題的網站 ``` ---- 想要更多資訊可以看由Littlepants學長寫的 [從零開始的競程介紹](https://hackmd.io/@LittlePants/Hyw_rueGK) --- ## 基本架構與輸入 ---- C++的基本架構 ```c++= #include<iostream>(標頭檔) using namespace std;(命名空間) int main(){ 主函式 return 0; }(結束執行) ``` ---- * 程式是由上往下執行 * 每打完一行程式必須用;作結尾 * 記得縮排,按tab鍵就能縮排 * //是註記,給人看的,電腦不會執行 * **學會排版,別人才看得舒服** --- 變數 === ---- ## 什麼是變數? ---- * 可以變動的數字 * 必須找容器儲存 ---- 可以把他想成一個容器,每個盒子的形狀都不一樣 裝的東西也不同 ![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQaWNeQ4ldD2qJz3cqZXVj85WwUyR_Jn8OBgfATDqHm2RawYyhrVW7aEaDd6i5gIJqzAK8&usqp=CAU) ---- 要怎麼宣告變數呢? ---- 程式碼 ```cpp= [|4] #include <iostream> using namespace std; int main(){ int a; // 變數型態為int(整數數字) return 0; } ``` ---- 變數型態 > * __int__(4bit) : 整數(integer),範圍(-2^32+1 ~ 2^32-1) ex:2、4、12365、(-456) > * __float__(4bit) / __double__(8byte) : 浮點數,用double比較精確 ex:2.145、3.14159、0.5555555 > * __char__(4bit) : 字元(charactor),存單個字,使用單括號 '' ex:'a'、'2'、'$' > * __string__ : 字串,存一串句子,使用雙括號 "" ex:"C++ is easy la!" > * __bool__ : 布林值,'0'以外的數皆為true ex: True/False 、 10 ---- 命名規則 * 數字不能開頭 ex:1a、2b、3c * 不能是[關鍵字](http://kaiching.org/pydoing/cpp/cpp-keyword.html) ex: while 、 auto...... * 命名最好與題目相關或是你看得懂的名稱!! --- 基本輸入 cout === ---- 程式範例 ```cpp= [|4,7] #include <iostream> using namespace std; int main(){ cout << "Hello World!!"; // 輸入符號或文字必須要加"" or '' cout << 123;// 輸出數字不須加括號 return 0; } ``` ---- 換行 ``` cpp= [|4|5|6] #include <iostream> using namespace std; int main(){ cout << "Hello World!!\n"; cout << "Hello World!!" << "\n";//'\n'也可以 cout << "Hello World!!" << endl; // 不須用括號 return 0; } ``` ---- 練習看看 [MDjudge A001](http://mdcpp.mingdao.edu.tw/problem/A001) [MDjudge A002](http://mdcpp.mingdao.edu.tw/problem/A002) --- 輸入 Cin === ---- * 將需要的變數輸入進容器裡 * 使用右箭頭 >> * 可以重複輸入變數至相同的容器, 容器內的數會以最後輸入的為主 ---- 範例輸入&輸出 ```cpp= #include <iostream> using namespace std; int main(){ int a; // 設置容器 cin >> a ; // 輸入變數 10 cout << a << endl; // 輸出變數10 cin >> a ; //再輸入變數5 cout << a ; 輸出變數? return 0; } ``` ---- 賦值 等號左邊的內容會變成等號右邊的資料 ex: a = 10 b = True c = 'A' d = "hello" ---- 程式範例 ```c++= #include <iostream> using namespace std; int main(){ int a = 10; // a 被賦值為10 int b = 5; // b 被賦值為5 a = b; // a 被賦值為 b cout << a; 輸出5 return 0; } ``` ---- 練習看看 [MDjudge A003](http://mdcpp.mingdao.edu.tw/problem/A003) [MDjudge A008](http://mdcpp.mingdao.edu.tw/problem/A008) --- ## 算術運算子 ---- 有+、-、*、/、%(取餘數) 基本上跟數學一樣,但要注意除法是整數除法 ---- 範例程式碼 ``` cpp= #include <iostream> using namespace std; int main(){ int a=5,b=3; cout<<a+b<<endl;//8 cout<<a-b<<endl;//2 cout<<a*b<<endl;//15 cout<<a/b<<endl;//1 cout<<a%b<<endl;//2 return 0; } ``` --- ## 字元與字串 ---- 剛剛看到變數型態除了int 還有char、string ---- ## char ---- char就是一個"字元" 如a、;、\[、+...等 宣告要用 '' ---- 宣告方法 ```cpp= #include<iostream> using namespace std; int main(){ // 字元必須用 ' ' (單引號)。 char c = 'X'; // 字元可以是半形空白。 char c = ' '; // 或是全行空白。 char c = ' '; // 字元的使用可以是 Unicode 編碼、16進制、數字、文字或符號。 char c = 'X'; char c = 'x'; char c = (char)88; // 該方法為數字直接轉型,88 為 16 進制的 58 轉換而來。88 = 10 進制,58 = 16進制。 char c = '@'; char c = '\''; // 若要使用 ' 單引號的話,必須使用跳脫字元 \。 } ``` ---- ## ascii code ![ascii](https://hackmd.io/_uploads/H1sggcEa6.png) ---- 想想看 'A'+1會輸出多少 'A'+'1'會輸出多少 ---- ## 跳脫字元 ---- 如果我想輸出' 我可以直接cout<<''';嗎 ---- 編譯器會把前兩個'當成開頭結尾 ---- 在前面加 \ ```cpp= #include<iostream> using namespace std; int main(){ cout<<'\''; return 0; } ``` ---- 輸出特殊符號 ,這就是跳脫字元的功能。 以下列出一些常見的跳脫字元: ``` * \n:換行 * \t:tab(大約是4個空白鍵) * \\:反斜線 \ * \":雙引號 \" * \':單引號 \' * \0:null 空字元(不等於空白,它就是一個空的字元) * \b:backspace(後退一個字元,需再補上其他字元填補原本字元的位置) * \r:回到該行開頭(後續輸出內容會覆蓋原本該行內容) ``` ---- ## \n 與 <<endl ---- 上面提到, \n 代表的字元是換行字元。 ---- 不知道各位還記不記得,剛剛學到 cout 時 有講過 <<endl 這個東西,功能也是換行。 ---- 乍看之下,兩者的功能沒啥太大的差別 但如果輸出的資料量很大時 **<<endl 的速度會比 \n 慢上許多!** ---- 這是因為 <<endl 在輸出一個換行後 會再執行一個「清空緩衝區」的動作。 至於 \n 需要額外加上 <<flush 才會清空緩衝區 ---- 所以,以下兩行功能會是相同的 ``` cout << "\n" << flush; cout << endl; ``` ---- 這裡就不多解釋緩衝區是什麼意思了 有興趣再自行搜尋就好。 各位只需要記得 <<endl 會比 \n 多一步手續 執行速度也會比 \n 來的慢。 ---- 所以解題時也建議使用 \n 來取代 <<endl 避免造成 TLE (Time Limit Exceed)。 ---- 練習看看 [zerojudge e926](https://zerojudge.tw/ShowProblem?problemid=e926) [zerojudge d559](https://zerojudge.tw/ShowProblem?problemid=d559) ---- ## string ---- * 可以看成由許多字元組成 * 宣告要用 "" * 要引入<string> ---- 宣告方法 ```cpp= #include<iostream> using namespace std; int main(){ string str1="Hello"; cout<<st1; } ``` ---- 可以使用 size() 或 length() 來取得字串長度,使用 empty() 測試字串是否為空,使用 == 比較兩個字串的內容是否相同 ---- ```cpp= #include <iostream> #include <string> using namespace std; int main() { string str1; string str2 = "caterpillar"; cout << "str1 是否為空:" << str1.empty() << endl; cout << "str1 長度: " << str1.size() << endl; cout << "str2 長度: " << str2.size() << endl; cout << "str1 與 str2 內容是否相同:" << (str1 == str2) << endl; return 0; } ``` ---- 可以將字串指定給另一個字串,例如: ``` string str1 = "text1"; string str2 = "text2"; .... str1 = str2; ``` ---- 也可以將一個 C 風格的字串指定給 string,例如: ``` string name = "caterpillar"; char str[] = "Justin"; name = str; ``` ---- 但不能將一個 string 指定給字元陣列,例如: ``` char str[] = "Justin"; string name = "caterpillar"; str = name; // error ``` ---- 可以使用 + 運算子來串接字串,例如: ``` str1 = str1 + str2; str1 = str1 + "\n"; ``` ---- 要輸入字串可以使用以下兩種方法 1.cin 2.getline ---- getline範例 ```cpp= #include<iostream> #include<string> using namespace std; int main(){ string str1; getline(cin,str1); cout<<str1<<'\n'; return 0; } ``` ---- 差別: getline() 每次讀入資料時會讀到換行符號才停止 cin 讀完特定資料就停下來 ----

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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