# 陣列 + 迴圈複習 ## 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
{"metaMigratedAt":"2023-06-16T11:40:44.780Z","metaMigratedFrom":"YAML","title":"陣列+迴圈複習","breaks":true,"slideOptions":"{\"transition\":\"slide\",\"theme\":null}","contributors":"[{\"id\":\"4f731eff-9d88-41f4-af56-2e3e02f20cfc\",\"add\":577,\"del\":75},{\"id\":\"9e7d687a-83f2-4e8a-8ee6-8846394e69a5\",\"add\":5809,\"del\":292},{\"id\":\"68c94489-3c2e-4879-b847-e982f360b03c\",\"add\":2455,\"del\":208},{\"id\":\"efc43b79-1b19-4cb1-9b18-ce50fad56214\",\"add\":609,\"del\":294}]"}
    564 views