owned this note changed 4 years ago
Linked with GitHub

重複結構

2021/10/01 電算社第三堂社課


打疫苗還要上社課
.__________________.


迴圈


電算社的猴子社長要學數數,幫忙他輸出1~5吧


#include<iostream> using namespacce std; int main(){ cout << 1 << ' '; cout << 2 << ' '; cout << 3 << ' '; cout << 4 << ' '; cout << 5 << ' '; return 0; }

是不是覺得很麻煩呢
那如果要數1 -> 50 呢


#include<iostream> using namespacce std; int main(){ cout << 1 << ' '; cout << 2 << ' '; cout << 3 << ' '; cout << 4 << ' '; cout << 5 << ' '; cout << 6 << ' '; cout << 7 << ' '; cout << 8 << ' '; cout << 9 << ' '; cout << 0 << ' '; cout << 11 << ' '; cout << 12 << ' '; cout << 13 << ' '; cout << 14 << ' '; cout << 15 << ' '; cout << 16 << ' '; cout << 17 << ' '; cout << 18 << ' '; cout << 19 << ' '; cout << 20 << ' '; cout << 21 << ' '; cout << 22 << ' '; cout << 23 << ' '; cout << 24 << ' '; cout << 25 << ' '; cout << 26 << ' '; cout << 27 << ' '; cout << 28 << ' '; cout << 29 << ' '; cout << 30 << ' '; cout << 31 << ' '; cout << 32 << ' '; cout << 33 << ' '; cout << 34 << ' '; cout << 35 << ' '; cout << 36 << ' '; cout << 37 << ' '; cout << 38 << ' '; cout << 39 << ' '; cout << 40 << ' '; cout << 41 << ' '; cout << 42 << ' '; cout << 43 << ' '; cout << 44 << ' '; cout << 45 << ' '; cout << 46 << ' '; cout << 47 << ' '; cout << 48 << ' '; cout << 49 << ' '; cout << 50 << ' '; return 0; // 講師是太閒喔 }

可以看到,一條一條輸出非常沒有效率
那我們怎麼簡化這一坨程式呢

我們要輸出1 -> 50

已知起始值是 1

結束的條件是當數字到50

每一次的間隔是 1 (1,2,3,4,5)

那我們就可以寫出下列的程式


#include<iostream> using namespace std; int main(){ int i = 1; while(i <= 50) // 當 i < = 50 時繼續執行 { cout << i << ' '; i++; // i+=1 } return 0; }

迴圈的功能

迴圈就是讓同一件事情可以重複運作

當你發現需要做的事情有規律的時候,

或是同一件事要做很多遍的時候,

就可以用迴圈


while迴圈


a//先做的事 while(b){//能夠繼續執行的條件 , b為 bool c//要執行的事 }


Examples

#include<iostream> using namespace std; int main(){ int i = 0; while(i <= 20){ cout << i << ' '; i += 5; } return 0; } //輸出:0 5 10 15 20

if() vs while()

int a = 5 , b = 9; if(a <= b) cout << "yes "; // yes while(a <= b) cout << "yes "; // yes yes yes yes yes

for迴圈


for(a; b; d){ c//迴圈內要做的事; }

當碰到for迴圈的時候,
程式會先執行a,接著判斷b是否成立,
若成立就執行c和d,否則就跳出迴圈

a只會執行一次


Examples

#include<iostream> using namespace std; int main(){ for(int i = 2; i <= 18; i += 4){ cout << i << ' '; } return 0; } //輸出:2 6 10 14 18

for 跟 while 的轉換

for(int i = 0 ; i < 10 ; i++){ cout << i << " \n"; } //上面的 for 迴圈其實等於下面的 while 迴圈 int i = 0; while(i < 10){ cout << i << " \n"; i++; }

do_while


do{ ...//迴圈內要做的事 }while(能夠繼續執行的條件); //注意分號!!!!!!!

與while不同的是,do_while是先執行命令再判斷
而while是先判斷再執行命令
因此do_while至少會執行一次命令


Examples

#include<iostream> using namespace std; int main(){ int N = 100; do{ cout << N << '\n'; }while(N < 10); return 0; } //輸出 : 100

你會發現,即便N不小於10,他還是會輸出一次N


break 、 continue


break : 強制退出迴圈

for(int i = 0 ; i < 10 ; i++){ if(i == 5){ break; } cout << i << ' '; } //輸出 : 0 1 2 3 4

continue : 終止這層迴圈,執行下一次

for(int i = 0 ; i < 10 ; i++){ if(i == 5){ continue; } cout << i << ' '; } //輸出 : 0 1 2 3 4 6 7 8 9

巢狀迴圈


和巢狀if一樣的意思,就是迴圈包著迴圈


while(a){ while(b){ ...//a成立且b成立時執行的事 } ...//a成立且b不成立時執行的事 } for(a; b; c){ for(d; e; f){ ...//b成立且e成立時執行的事 } ...//b成立且e不成立執行的事 }

Examples

#include<iostream> using namespace std; int main(){ for(int i = 0; i < 3; i++){ for(int j = 0; j < 4; j++){ cout << i << ' ' << j << endl; } cout << endl; } return 0; } /* 輸出: 0 0 0 1 0 2 0 3 1 0 1 1 1 2 1 3 2 0 2 1 2 2 2 3 */

連續輸入


有時候會需要一次輸入很多項,這時候就可以利用迴圈達成目的


Examples

#include<iostream> using namespace std; int main(){ int n;//要連續輸入n項 cin >> n; int a, b; for(int i = 0; i < n; i++){ cin >> a >> b; cout << a + b << ' '; } } /* 輸入: 2 1 5 6 3 */ //輸出:6 9

小練習

電算社的猴子社長要來學九九乘法表了,你可以幫忙他輸出九九乘法表嗎


輸入說明:沒有輸入
輸出說明:輸出九九乘法表,每輸出一項即換行

範例輸入:沒有輸入
範例輸出:
2 * 1 = 2
2 * 2 = 4

9 * 8 = 72
9 * 9 = 81


我是防雷頁:D


解答

#include<iostream> using namespace std; int main() { for(int i = 2; i <= 9; i++){ for(int j = 1; j <= 9; j++){ cout << i << " * " << j << " = " << i * j << '\n'; } } return 0; }

OJ練習

(依照難度)

  1. GreenJudge a021 我討厭偶數
  2. GreenJudge a037 公平的戰役(N行版)
  3. GreenJudge a027 倒數計時
  4. GreenJudge a023 3N+1
  5. GreenJudge a024 所有位數和
  6. GreenJudge a025 數字倒轉
  7. ZeroJudge a005 Eva的回家作業
Select a repo