CPE數學系
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    {%hackmd @hipp0\Hippotumuxthem %} <style> .text-center{ text-align: center; //文字置中 } .text-left{ text-align: left; //文字靠左 } .text-right{ text-align: right; //文字靠右 } </style> <style> .reveal .slides { text-align: left; font-size:30px; } </style> # C++ 基礎II --- <h2 style='color:#C4C400'> 複習 </h2> ---- <h3 style='color:#C4C400'> 變數宣告: </h3> ```cpp= int a = 1; long long b = 2; char c = 'a'; double d = 2.564; // 不要用 float ! string e = "ABC"; bool ck = 0; //false ``` ---- <h3 style='color:#C4C400'> 注意事項 </h3> - 請注意: int 範圍 (1e9) 如果超過請用 long long - char 字元使用 'c' 表示, string 字元 "hi oj" - 使用 string 而不使用 char[] ---- <h3 style='color:#C4C400'> acill </h3> - 注意字元是 '' 字串是 "" - 不用打 97 來代表 a ,可以直接用 'a' 就好 ```cpp= char c; cin >> c; if ('a' <= c && c <= 'z') cout << "lower" << endl; ``` ---- <h2 style='color:#C4C400'> 額外題目 X 2 </h2> ---- <h2 style='color:#C4C400'> 你快樂嗎? </h2> - 型態判斷 ---- <h2 style='color:#C4C400'> 進階天數判斷 (不要用 if else) </h2> - true(1) false(0) --- <h2 style='color:#C4C400'> 迴圈 </h2> ---- <h2 style='color:#C4C400'> for </h2> 最常用的迴圈,語法如下: ```cpp= for (int i = 0 ; i < 100 ; i++){ cout << i << '\n' ; // do things } ``` ---- <h3 style='color:#C4C400'> 語法 </h3> ```cpp= for (1 ;2 ;4) { // 3 do } ``` - 1: 程式執行到迴圈時,第一件會做的事情 - 2: 程式進行的條件,如果不滿足,便會離開這個迴圈。 - 3: 迴圈裡面做什麼事情 - 4: 做完一件事情後,迴圈會做的事 ---- <h3 style='color:#C4C400'> 反過來? </h3> ```cpp= string a = "1234567890"; for (int i = a.length() - 1 ; i >= 0 ; i--) { cout << a[i]; } ``` ---- <h3 style='color:#C4C400'> 注意事項 </h3> - 如果是用在陣列裡,記得範圍 (尤其反向的時候) - 如果記得如果反向,要-1,如果寫成 +1 會無窮 ---- <h3 style='color:#C4C400'> 巢狀迴圈</h3> (多層迴圈) ```cpp= for (int i = 0 ; i < 10 ; i++) { for (int j = 0 ; j < 20 ; j++) { // do ... } } ``` ---- <h3 style='color:#C4C400'> 注意事項 </h3> - 記得變數要設不一樣,最後要++的部分也要寫對 ---- <h3 style='color:#C4C400'> 只想要條件的迴圈? </h3> ```cpp= int n = 0; for( ; n < 10 ; ){ int x; cin >> x; n += x; } ``` ---- <h2 style='color:#C4C400'> while </h2> 用法如下: ```cpp= while(條件){ //do } ``` ---- <h3 style='color:#C4C400'> 剛剛的例子 </h3> ```cpp= while(n < 10){ int x; cin >> x; n += x; } ``` ---- <h3 style='color:#C4C400'> 無窮迴圈 </h3> while 是條件成立會繼續執行,那如果條件本身就是 true 呢? ```cpp= while (true) { // do } ``` ---- 但是你會發現,這樣的迴圈無法停止,於是我們需要引入讓迴圈結束的工具 ---- <h2 style='color:#C4C400'> continue/break </h2> 當我們遇到某些條件成立時,想跳出迴圈 or 進下一輪 ---- <h3 style='color:#C4C400'> continue </h3> 用來結束這一輪,但是迴圈會繼續執行 ```cpp= int x; cin >> x; for (int i = 0 ; i < x ; i++) { if (i % 2 == 0) continue; else cout << i << endl; } ``` ---- <h3 style='color:#C4C400'> break </h3> 用來直接結束當前迴圈 ```cpp= int x, now = 0; cin >> x; while (true) { if (now * now * now == x) break; now ++; } cout << now << '\n'; ``` ---- <h3 style='color:#C4C400'> 注意事項 </h3> - 這兩個操作都只能夠離開當前的迴圈! - 用無窮迴圈記得要有 break 的離開條件 ---- <h3 style='color:#C4C400'> 離開當前的迴圈? </h3> 以下錯誤寫法 ```cpp= int ans1, ans2; for (int i = 0 ; i < 10 ; i++) { for (int j = i + 1 ; j < 10 ; j++) { if (i * j > 78) { ans1 = i, ans2 = j; break; } } } cout << ans1 << " " << ans2 << endl; ``` ---- <h3 style='color:#C4C400'> 正確寫法 </h3> ```cpp= int ans1, ans2; bool check = false; for (int i = 0 ; i < 10 ; i++) { for (int j = 0 ; j < 10 ; j++) { if (i * j > 78) { ans1 = i, ans2 = j; check = true; break; } } if (check) break; } cout << ans1 << " " << ans2 << endl; ``` ---- <h3 style='color:#C4C400'> 練習題 </h3> - B001 - B002 ---- ![image](https://hackmd.io/_uploads/BJcJcL1A6.png) --- <h2 style='color:#C4C400'> 題目的各種輸入方式 </h2> ---- <h3 style='color:#C4C400'> t筆輸入 </h3> ```cpp= int t; cin >> t; while (t--) { // do } // 如果題目要求輸出 case i for (int i = 1 ; i <= t ; i++) { cout << "Case " << i << " "; // do } ``` ---- <h3 style='color:#C4C400'> 輸入直到讀到某個值 </h3> 例如讀到 -1 為止 ```cpp= int t; cin >> t; while (true) { if (t == -1) break; // do cin >> t; } ``` ---- <h3 style='color:#C4C400'> 輸入直到 EOF </h3> EOF = end of file ```cpp= int t; while (cin >> t) { // do } ``` ---- <h3 style='color:#C4C400'> 輸出 n 個數字用空白隔開 </h3> 要求: 尾巴不能有空白 ```cpp= int ans[10] = {2,3,4,1,5,6,7,1,2,3}; bool first = true; for (int i = 0 ; i < 10 ; i++){ if (first) { cout << ans[i]; first = false; }else{ cout << " " << ans[i]; } } cout << '\n'; ``` ---- 這只是幾種常見的輸入輸出而已,還是要看題目的敘述為主。 --- <h2 style='color:#C4C400'> 陣列 </h2> 如果要儲存5個變數,我們可能會這樣做 ```cpp= int a, b, c, d, e; ``` 但如果100、1000個變數那怎麼辦呢? ---- <h3 style='color:#C4C400'> 宣告 </h3> ```cpp= int a[15]; //a[0]~a[14] 共15個 char b[150]; //b[0]~b[149] 共150個 double c[200]; //c[0]~c[199] 共200個 string str[1500]; //str[0]~str[1499] 共1500個 ``` ---- 可以一次定義所有數字 or 部分數字 ```cpp= int a[5] = {1,2,3,4,5}; int b[100] = {1,2,3}; // 其餘會自動補 0 int zero[1000] = {0}; ``` ---- <h3 style='color:#C4C400'> 使用 </h3> ```cpp= int a[5] = {1,2,3,4,5}; cout << a[0] + a[2] << '\n'; a[3] = 6; cout << a[1] + a[3]; for (int i = 0 ; i < 5 ; i++) { // 注意 是 0~4 cout << a[i] << ' '; } cout << '\n'; ``` ---- <h3 style='color:#C4C400'> 注意事項 </h3> - 陣列的 index (索引值) 是從 0 開始到 size - 1,引用超過會報 RE or WA。 - 陣列宣告過後,不可改變大小或重新宣告。 - 如果在裡面宣告沒有給定初始值,他不會自動幫你補 0 !!!!!! - 陣列大小大概只能開到 2e6 左右 ---- <h3 style='color:#C4C400'> 多維陣列 </h3> 陣列是可以有多個維度的,例如: ```cpp= int arr[13][14][15] = {0}; // 設0, cin >> arr[2][5][6]; cout << arr[2][5][6] << '\n'; // 也可以這樣定義,一樣不夠的會自動補 0 int arr[2][3] ={{1,2,3},{4,5,6}}; ``` ---- ![image](https://hackmd.io/_uploads/H1IhjNeRa.png) ---- <h2 style='color:#C4C400'> string </h2> ---- <h3 style='color:#C4C400'> 宣告 </h3> ```cpp= #include <string> #include <iostream> using namespace std; int main(void) { string s; cin >> s; for (int i = 0 ; i < s.length() ; i++) { // do... } } ``` ---- <h3 style='color:#C4C400'> 好用的加法 </h3> ```cpp= string a = "123", b = "456"; string c = a + b; // 123456 cout << c << '\n'; ``` ---- <h3 style='color:#C4C400'> 好用的翻轉 string </h3> ```cpp= #include <algorithm> // 記得記得要加 #include <string> #include <iostream> using namespace std; int main(void) { string s = "123456"; reverse(s.begin(), s.end()); } ``` ---- <h3 style='color:#C4C400'> 酷酷的判斷 </h3> 字串判斷大小是根據字典序排序,也就是根據 acill 碼來比較大小 他會先判斷最前面,依序往後面判斷 假設 a = "1234" b = "567" 則 b 比較大 假設 a = "12345" b = "123" 則 a 比較大 可以直接用 判斷寫法寫 ```cpp= string a = "abcd", b = "fghi"; cout << (a > b) << '\n'; // false ``` ---- <h3 style='color:#C4C400'> 酷酷的標頭檔 (懶人) </h3> ```cpp= #include <bits/stdc++.h> ``` 包含 ![image](https://hackmd.io/_uploads/r1gAuVg06.png) ---- <h3 style='color:#C4C400'> 輸入輸出優化 </h3> 檢定不會用到,但我提一下 請注意: 不要使用 endl 會導致使用 flush 而無法加快,請使用 '\n' ```cpp= #include <bits/stdc++.h> using namesapce std; int main(void) { ios::sync_with_stdio(false),cin.tie(0); } ``` ---- <h3 style='color:#C4C400'> 練習題 </h3> - B003 - B004 ---- ![image](https://hackmd.io/_uploads/BJcJcL1A6.png) --- <h2 style='color:#C4C400'> function </h2> ---- <h3 style='color:#C4C400'> 例子 </h3> ```cpp= #include <bits/stdc++.h> using namespace std; void hello() { cout << "hello world\n"; } int main(void) { hello(); } ``` ---- <h3 style='color:#C4C400'> 引入變數 </h3> ```cpp= void two_sum (int a, int b) { cout << a + b << '\n'; } int main(void) { int x1, x2; cin >> x1 >> x2; two_sum (a, b); } ``` ---- <h3 style='color:#C4C400'> 回傳不同型態 </h3> return - void 不回傳 - int\char\string .... 回傳該型態 ```cpp= void nothing() { return; } int two_sum (int a, int b) { return a + b; } int main(void) { int x1, x2; cin >> x1 >> x2; cout << two_sum(x1,x2); } ``` ---- <h3 style='color:#C4C400'> 前後定義的問題 </h3> ```cpp= int main(void) { int x1, x2; cin >> x1 >> x2; cout << two_sum(x1,x2); } int two_sum (int a, int b) { return a + b; } ``` ![image](https://hackmd.io/_uploads/SJ8ouBxR6.png) ---- <h3 style='color:#C4C400'> 解決方法 </h3> - 定義在前面 - 跟變數一樣 先宣告 ```cpp= int two_sum (int a, int b); int main(void) { int x1, x2; cin >> x1 >> x2; cout << two_sum(x1,x2); } int two_sum (int a, int b) { return a + b; } ``` ---- <h3 style='color:#C4C400'> 我喜歡的寫法 </h3> ```cpp= #include <bits/stdc++.h> using namespace std; void solve() { // do } int main(void) { int t = 1; // cin >> t; while (t--) solve(); } ``` ---- <h3 style='color:#C4C400'> 我喜歡的寫法 EOF 版本 </h3> ```cpp= #include <bits/stdc++.h> using namespace std; void solve(int t) { // do } int main(void) { int t; // 看題目輸入什麼型態 while (cin >> t) solve(t); } ``` ---- <h3 style='color:#C4C400'> why </h3> - 假設今天我們在好幾層迴圈的時候,得到答案想要離開 我們可能需要這樣寫 ```cpp= bool check = false; for (int i = 0 ; i < n ; i++) { for (int j = 0 ; j < m ; j++) { for (int k = 0 ; k < p ; k++) { if (...) { cout << ans << '\n'; check = true; break; } } if (check) break; } if (check) break; } ``` ---- <h3 style='color:#C4C400'> 可以直接 return </h3> ```cpp= for (int i = 0 ; i < n ; i++) { for (int j = 0 ; j < m ; j++) { for (int k = 0 ; k < p ; k++) { if (...) { cout << ans << '\n'; return; } } } } ``` --- <h2 style='color:#C4C400'> 區域、全域變數 </h2> ---- <h3 style='color:#C4C400'> 區域變數 </h3> 起始於變數宣告,結束於宣告敘述所在的區塊的大右括號。 ```cpp= #include <bits/stdc++.h> using namespace std; int func(int a, int b) { int c = 5; return c*(a+b); // abc都屬於func的區域變數 (和main的abc不衝突) }; int main(void){ int a, b; //屬於main函式的區域變數 cin >> a >> b; cout << func(a,b) << '\n'; for (int i = 0 ; i < 3 ; i++) { int x; //出了for迴圈之後,x就結束了 cin >> x; cout << x << '\n'; } } ``` ---- <h3 style='color:#C4C400'> 全域變數 </h3> - 宣告在所有區塊和類別之外的變數 - 不可宣告同名的全域變數 - 若沒有給定初始值,會自動給0 ---- <h3 style='color:#C4C400'> 全域變數 </h3> ```cpp= #include <bits/stdc++.h> using namespace std; int arr[10000000]; //全域變數,所以沒有給定初始值會自動給0 int main(void){ int str[100000]; //這邊是區域變數,沒有給定初始值就不會有 } ``` ---- <h3 style='color:#C4C400'> 注意事項 </h3> - 定義名稱不要重複 - 陣列大小區域大概 (2e6) 全域大概可以開到 (5e7) - 全域變數會自動給 0 但區域變數不會!! ---- <h3 style='color:#C4C400'> 練習題 </h3> - B005 ----

    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