Tony041010
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 2021CRC title: 陣列+迴圈複習 slideOptions: transition: slide theme: --- # 陣列 + 迴圈複習 ## 2021/10/08 電算社第四堂社課 --- ## 迴圈複習 ---- 還記得猴子社長的故事嗎 :D 歡迎猴子社長的回歸(x ---- 電算社的猴子社長又要來學數數了,幫忙輸出1~100吧 ---- while迴圈寫法 ```cpp= #include<iostream> using namespace std; int main(){ int i = 1; while(i <= 100){ cout << i << ' '; i++; } return 0; } ``` **注意:while迴圈是先判斷再執行** ---- for迴圈寫法 ```cpp= #include<iostream> using namespace std; int main(){ for(int i = 1; i <= 100; i++){ cout << i << ' '; } return 0; } ``` ---- do_while迴圈寫法 ```cpp= #include<iostream> using namespace std; int main(){ int i = 1; do{ cout << i << ' '; i++; }while(i <= 100); return 0; } ``` **注意:do_while迴圈是先執行再判斷** --- ## 多筆測資輸入: 輸入兩個數字,幫他們比大小 ex 輸入: 1 3 輸出: 1 < 3 輸入: 3 3 輸出: 3 = 3 ---- ### 1行: ```cpp= #include<iostream> using namespace std; int main() { int a , b; cin >> a >> b; if(a > b) cout << a << " > " << b; else if(a < b) cout << a << " < " << b; else cout << a << " = " << b; } ``` ---- ### 多行: ```cpp= /* 輸入: 2 1 3 3 3 輸出: 1 < 3 3 = 3 */ #include<iostream> using namespace std; int main() { int a , b , n; cin >> n; //代表有幾筆資料 for(int i=0 ; i<n ; i++) { cin >> a >> b; if(a > b) cout << a << " > " << b << endl; else if(a < b) cout << a << " < " << b << endl; else cout << a << " = " << b << endl; } } ``` ---- ### 0尾: ```cpp= /* 輸入: 1 3 3 3 0 0 輸出: 1 < 3 3 = 3 */ #include<iostream> using namespace std; int main() { int a , b; while(cin >> a >> b) { if(a == 0 && b == 0) break; if(a > b) cout << a << " > " << b << endl; else if(a < b) cout << a << " < " << b << endl; else cout << a << " = " << b << endl; } } ``` ---- ### EOF版 ```cpp= /* 輸入: 1 3 3 3 輸出: 1 < 3 3 = 3 */ #include<iostream> using namespace std; int main() { int a , b; while(cin >> a >> b) { if(a > b) cout << a << " > " << b << endl; else if(a < b) cout << a << " < " << b << endl; else cout << a << " = " << b << endl; } } ``` --- ## 陣列 ---- 電算社的猴子社長要點名 :D, 他想要用程式輸入所有人的名字,並把它紀錄下來。 ---- 雖然用迴圈就可以重複輸入,但是電算社這一屆有130多個人, 如果要用string宣告,那要宣告130多個變數? ---- ```cpp= #include<iostream> using namespace std; int main(){ string s1 = "1"; string s2 = "2"; string s3 = "3"; string s4 = "4"; string s5 = "5"; string s6 = "6"; string s7 = "7"; string s8 = "8"; string s9 = "9"; string s10 = "10"; string s11 = "11"; string s12 = "12"; string s13 = "13"; string s14 = "14"; string s15 = "15"; string s16 = "16"; string s17 = "17"; string s18 = "18"; string s19 = "19"; string s20 = "20"; string s21 = "21"; string s22 = "22"; string s23 = "23"; string s24 = "24"; string s25 = "25"; string s26 = "26"; string s27 = "27"; string s28 = "28"; string s29 = "29"; string s30 = "30"; string s31 = "31"; string s32 = "32"; string s33 = "33"; string s34 = "34"; string s35 = "35"; string s36 = "36"; string s37 = "37"; string s38 = "38"; string s39 = "39"; string s40 = "40"; string s41 = "41"; string s42 = "42"; string s43 = "43"; string s44 = "44"; string s45 = "45"; string s46 = "46"; string s47 = "47"; string s48 = "48"; string s49 = "49"; string s50 = "50"; string s51 = "51"; string s52 = "52"; string s53 = "53"; string s54 = "54"; string s55 = "55"; string s56 = "56"; string s57 = "57"; string s58 = "58"; string s59 = "59"; string s60 = "60"; string s61 = "61"; string s62 = "62"; string s63 = "63"; string s64 = "64"; string s65 = "65"; string s66 = "66"; string s67 = "67"; string s68 = "68"; string s69 = "69"; string s70 = "70"; string s71 = "71"; string s72 = "72"; string s73 = "73"; string s74 = "74"; string s75 = "75"; string s76 = "76"; string s77 = "77"; string s78 = "78"; string s79 = "79"; string s80 = "80"; string s81 = "81"; string s82 = "82"; string s83 = "83"; string s84 = "84"; string s85 = "85"; string s86 = "86"; string s87 = "87"; string s88 = "88"; string s89 = "89"; string s90 = "90"; string s91 = "91"; string s92 = "92"; string s93 = "93"; string s94 = "94"; string s95 = "95"; string s96 = "96"; string s97 = "97"; string s98 = "98"; string s99 = "99"; string s100 = "100"; string s101 = "101"; string s102 = "102"; string s103 = "103"; string s104 = "104"; string s105 = "105"; string s106 = "106"; string s107 = "107"; string s108 = "108"; string s109 = "109"; string s110 = "110"; string s111 = "111"; string s112 = "112"; string s113 = "113"; string s114 = "114"; string s115 = "115"; string s116 = "116"; string s117 = "117"; string s118 = "118"; string s119 = "119"; string s120 = "120"; string s121 = "121"; string s122 = "122"; string s123 = "123"; string s124 = "124"; string s125 = "125"; string s126 = "126"; string s127 = "127"; string s128 = "128"; string s129 = "129"; string s130 = "130"; string s131 = "131"; string s132 = "132"; string s133 = "133"; string s134 = "134"; string s135 = "135"; } ``` ---- 我們可以看到這個方式有很多缺點 1.沒有人會想這樣寫程式 2.如果這個時候社團多了一個人, 我就要再多打一行 string sx = "x"; 比起宣告130個變數,<font color="FFF300">如果能宣告一個變數, 讓他儲存130個資料很明顯比較輕鬆</font>, 沒錯,這就是陣列的功能。 ---- 使用方式: ```cpp= string name[135]; //宣告 name[0] = "猴子社長"; //呼叫 ``` ---- 宣告: string name[size]; string 為陣列中資料的型態 name是這個陣列的名字 [size] 是這個陣列的大小 (從 1 開始) 使用: name[n] 第n項的值 0 <= n <= number-1 **注意:** 它是從0開始計算, [0] 指的是第一項 --- ## 陣列 * 迴圈 ---- 那我們要怎麼把所有人的名字都輸入進去呢 ---- ```cpp= #include<iostream> using namespace std; int main() { string arr[135]; for(int i = 0 ; i < 135 ; i++){ cin >> arr[i]; //動態宣告 } return 0; } ``` 這樣就把所有人的名字都輸進來了:D ---- Example: 開一個大小為 10 的陣列, 並且輸入10個數 $a_i$ 進去,(0 <= $a_i$ <= $10^5$) 並輸出10個數的總和 那要怎麼做呢? ---- ```cpp= #include<iostream> using namespace std; int main(){ int num[10]; //總共10個數 int sum = 0; //一開始總和為0 for(int i = 0; i < 10; i++){ cin >> num[i]; //輸入10個數 sum += num[i]; //加起來:D } cout << sum; return 0; } ``` --- ## sort ```cpp= #include<algorithm> //std sort的資料庫 ``` ---- sort是將一個陣列由小到大排列 ```cpp= #include<iostream> #include<algorithm> using namespace std; int main() { int arr[100]; int a = 100; // 因為只有一行所以for不用{} for(int i = 0 ; i < a ; i++) cin >> arr[i]; sort(arr , arr + a); // arr為起始位置 , arr + a為結束位置(不含第arr + a個) for(int i = 0 ; i < a ; i++) cout << arr[i] << ' '; return 0; } ``` ---- Example: 給你輸入一些數字,你可以幫忙輸出這些數字由小到大的排列嗎 ---- ```cpp= #include<iostream> #include<algorithm> using namespace std; int main(){ int n; cin >> n; //總共會輸入幾個數字 int arr[n]; //要排列的數字 for(int i = 0; i < n; i++){ cin >> arr[i]; //輸入 } sort(arr, arr + n); //由小到大排列 for(int i = 0; i < n; i++){ cout << arr[i] << ' '; //輸出 } return 0; } ``` --- ### 小練習 大雄想要練習他的記憶力,並和胖虎打賭說如果給他一些數字, 他可以把這些數字記起來並倒過來念,但他突然意識到他做不到, 身為哆啦A夢的你,可以幫忙大雄以避免他又再一次的被胖虎毆打嗎? ---- 輸入說明:先輸入一個整數n,代表胖虎要給大雄的數字數量,接著再輸入n個整數 輸出說明:輸出倒過來的數字 範例輸入: 5 4 8 7 6 3 範例輸出: 3 6 7 8 4 ---- 我是防雷頁:D ---- ```cpp= #include<iostream> using namespace std; int main(){ int n; cin >> n; int num[n]; for(int i = 0; i < n; i++){ cin >> num[i]; } for(int i = n - 1; i >= 0; i--){ cout << num[i] << ' '; } return 0; } ``` --- ### OJ練習 ### 輸入輸出練習 1. [GreenJudge a036 公平的戰役(1行版)](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a036) 2. [GreenJudge a037 公平的戰役(N行版)](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a037) 3. [GreenJudge a038 公平的戰役(0尾版)](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a038) 4. [GreenJudge a039 公平的戰役(EOF版)](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a039) ---- 1. [GreenJudge b002 找最大值](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=b002) 2. [GreenJudge b003 資料分組](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=b003) 3. [GreenJudge b004 一個都不能少](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=b004) 4. [GreenJudge b005 熱門點播](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=b005) --- 段考加油:D

    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