--- ###### tags: `課程` --- 迴圈(For and While) === --- ## 座位表 | | 講桌 | 講桌 | | | ------ |:------:|:------:| ------ | | 408_32 | 403_22 | 505_38 | 406_04 | | 504_17 | 401_35 | 404_29 | 404_22 | | 505_27 | 506_29 | 505_35 | 505_30 | | | | | | | 508_37 | 503_21 | 407_24 | 507_12 | | 408_33 | 506_27 | 408_30 | 505_29 | | 505_16 | 405_12 | 403_15 | 508_42 | --- # 迴圈(For and While) <a href="https://reurl.cc/vgLVMA">https://reurl.cc/vgLVMA</a> <a href="https://reurl.cc/vgLVMA">![](https://i.imgur.com/zMRtRo7.png)</a> --- ## While迴圈 ---- ### 執行步驟 1. 當條件符合(true),就執行大括號內的程式 2. 回到小括號內再次判斷: 符合(true)-->執行下方指令 不符合(false)-->結束迴圈跳出 ### 格式 ```cpp while ( 執行條件 ){ 要執行的指令; } ``` ---- ## 接下來複習一下IF結構 ### If格式 ```cpp if ( 執行條件 ){ 要執行的指令; } ``` ---- ## If v.s. While | |**If迴圈** |**While迴圈** | | ----------- | --------- | ----------------- | | **條件符合後** | 執行一次 | 一直執行到條件不符合 | ![](https://i.imgur.com/tMnCcdL.png) ---- ### 範例:印出1~10的數字 ```cpp= #include<iostream> using namespace std; int main(){ int i = 1; while (i <= 10){ cout << i << endl; i++; } return 0; } ``` ---- ### 範例:印出5X5的「\*」(如下) ```***** ***** ***** ***** ***** ***** ``` https://reurl.cc/vgLVMA ---- ```cpp= #include <iostream> using namespace std; int main() { int i = 1; while (i <= 5) { int j = 1; while (j <= 5) { cout << "*"; j++; } cout << endl; i++; } return 0; } ``` ---- ![](https://i.imgur.com/gGc5D1T.png) #### 程式: ```cpp while ( 等待時間 >= 20分鐘 ) { 跟罹患阿茲海默症的阿嬤要錢; } ``` --- ## For迴圈 ---- ### 執行步驟 1. 設定初始值,接著進行判斷 2. 當條件符合(true),就執行大括號內的程式 3. 回到小括號執行第二個分號後的程式(運算式) 4. 再次進行判斷: 符合(true)-->回到第二步 不符合(false)-->結束迴圈並跳出 ### 格式 ```cpp for( 初始化 ; 執行條件 ; 運算式 ){ 要執行的指令; } ``` ---- ### 範例:輸出3次「HI」 ```cpp= #include<iostream> using namespace std; int main (){ for( int i = 0; i < 3; i++ ){ cout << "HI" << endl; } return 0; ] ``` #### ⊳小提醒:宣告的 i 作用範圍只有迴圈內喔! ---- ### 範例:印出1~10 ```cpp= #include<iostream> using namespace std; int main (){ for(int i = 0; i < 10; i++){ cout << i + 1 << endl; } return 0; } ``` ---- ### 範例:印出5X5的「\*」 ---- ```cpp= #include<iostream> using namespace std; int main (){ for(int i = 0 ; i < 5 ; i ++){ for(int j = 0 ; j < 5 ; j++){ cout<<'*'; } cout<<endl; } return 0; } ``` --- ## For v.s. While ---- | | For | While | | -------- | -------- | -------- | | 初始化 | for(<font color="#f00">here</font>; xxxx; xxxx) | 自行在外面設定 | |判斷條件 |for(xxxx; <font color="#f00">here</font>; xxxx)|while(<font color="#f00">here</font>)| |迴圈跑完後執行 |for(xxxx; xxxx; <font color="#f00">here</font>)|自行在大括號內設定| |差異|迴圈有固定執行次數|迴圈中改變執行次數| |優點|新手容易debug|使用彈性高| |<font color="#f00">**注意**</font>|**盡量不要在裡面修改結束條件**|**要確保能夠結束**| --- ## 進階用法 ---- ## while cin ---- 當我們要計算a+b=?時,而題目沒有說會給多少次輸入 則可以使用以下語法 ```cpp= //Question: a+b=? #include <iostream> using namespace std; int main(){ int a, b; while(cin >> a >> b) cout << a + b <<endl; return 0; } ``` ---- ### 不斷輸入直到輸入「-1」 ```cpp= #include <iostream> using namespace std; int main(){ int a; while(cin >> a && a != -1) cout << a <<endl; return 0; } ``` ---- ## do...while 1. 先執行大括號內的程式 2. 進到小括號內判斷: 符合(true)-->重新執行大括號內指令 不符合(false)-->結束迴圈並跳出 ### 格式 ```cpp do{ 要執行的指令; }while ( 執行條件 ); ``` ---- ### do while特色 ### 一定會執行一次大括號內的指令 --- ## 練習時間 --- ## 數字塔 給定數字n 印出如圖中的數字塔(n < 10) ``` 11 2222 333333 44444444 5555555555 ``` https://reurl.cc/vgLVMA ---- ### While迴圈作法 ```cpp= #include <iostream> using namespace std; int main() { int a = 1, b, n, d; cin >> n; while (a <= n) { //a = 要輸出的數字 b = n - a; //b = 要輸出的空格數 while (b > 0) { // \* cout << " "; // |輸出空格 b--; // | } // /* d = a*2; //d = a要輸出的次數 while (d > 0) { // \* cout << a; // |輸出a d--; // | } // /* cout << endl; a++; } return 0; } ``` ---- ### For迴圈作法 ```cpp= #include <iostream> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n - i - 1; j++) cout << ' '; for (int j = 0; j < (i + 1) * 2 ; j++) cout << i + 1; cout << endl; } return 0; } ``` --- ## 給定a,b求 a^b^ ``` example: input : 5 3 output: 125 ``` https://reurl.cc/vgLVMA ---- ```cpp= #include<iostream> using namespace std; int main (){ int a, b, ans = 1; //ans(總數)的初始值要設為1,不然0乘以多少都是0 cin >> a >> b; for(int i = 0; i < b; i++ ){ ans = ans * a; } cout << ans << endl; return 0; } ``` ---- ### 進階算法 fastpow ```cpp= int main(){ int a, b, ans = 1; cin >> a >> b; while(b){ if(b & 1) ans = ans * a; a *= a, b >>= 1; } cout << ans <<endl; return 0; } ``` 程式中的<font color="blue"> b & 1 </font>會等於<font color="blue"> b </font>二進位後2^0^的數值,也就等同於判斷<font color="blue"> b </font>是否為奇數 ---- ### 白話版 ```cpp= int main(){ int a, b, ans = 1; cin >> a >> b; while(b != 0){ if(b % 2 == 1) ans = ans * a; a = a * a; b = b >> 1; } cout << ans <<endl; return 0; } ``` 程式中的<font color="blue"> >> 1 </font>是位元右移運算符號,代表二進位後數值向右移1位,也就是最右邊捨去一位 --- ## 輸入一個數,問是否為質數 ## 並找出它的所有因數 ``` excample: input :20 output: 1 2 4 5 10 20 no ``` https://reurl.cc/vgLVMA ---- ```cpp= #include <iostream> using namespace std; int main() { int a, t = 0; cin >> a; for (int i = 1; i <= a; i++) { if (a % i == 0) { cout << i << " "; t++; } } cout << endl; if (t > 2) cout << "no" << endl; else cout << "yes" << endl; return 0; } ``` --- ## 補充用法--使用define縮短程式 ```cpp= #include <iostream> #define loop(n) for(int i=0;i<n;i++) using namespace std; int main() { int a; cin >> a; loop(a){ cout << "*"; } return 0; } ``` ---- ## 補充用法--break 跳出迴圈 https://www.csie.ntu.edu.tw/~b98902112/cpp_and_algo/cpp/loop_break.html 當滿足中斷條件時,就離開迴圈( while 或 for ) #### 形式 ```cpp while / for( ... ) { ... if( 中斷條件 ) { break; } ... } ``` ---- ## 範例 ```cpp= #include<iostream> using namespace std; int main() { int i = 0; while( 1 ) { cout << i << endl; if( i>10 ) { break; } i = i+1; } return 0; } ``` ---- ### 輸出結果 ``` 0 1 2 3 4 5 6 7 8 9 10 11 ``` --- ## 更多的題目2ㄏ2ㄏ 整數n的階乘(n!)是連續整數1到n的乘積。例如,5階乘的計算為: 5! = 5 * 4 * 3 * 2 * 1 = 120 請撰寫一個程式並顯示前10個階乘的列表。 ㄏㄏ ---- ## 解答ㄟ ```cpp= #include <iostream> using namespace std; int main (){ int a,ans=1; cin>>a; for(int j=1;j<=a;j++){ ans*=j; } cout<<ans; } ``` --- ## 又是更多的題目qq 請撰寫一程式以產生和顯示 n 和 ^2 的列表,n 的範圍從1到10。 ---- ## 解答ㄟ哭歐 ```cpp= #include <iostream> using namespace std; int main (){ int i = 0; for ( int a = 0; a <= 10 ; a ++){ cout<< " " <<a; } cout<<endl; for ( int a = 0; a <= 10 ; a ++){ i = a * a; cout<< " " <<i; } } ``` --- ## 我真的覺得公關超級報幹溪帥的 現行的曆法是從羅馬人的曆法演變而來的。凱撒大帝編纂了一套曆法,後人稱之為儒略曆 (Julian calendar)。在這曆法中,除了四、六、九、及十一月有30天,二月在平年有28天,在閏年有29天以外,其他的月份都是31天。再者,在這曆法中,每四年有一個閏年。這導因於古代羅馬的星象學家算出一年有365.25天,因此每隔四年就要加一天以保持曆法和季節的一致。於是,他們就在四的倍數的年份多加了一天 (二月29日)。 ---- 儒略法: 四的倍數的年份均為閏年,這年會多一天 (二月29日)。 在1582年,教宗格瑞哥里 (Gregory) 的星象學家發現一年並不是365.25天,而是比較接近365.2425天。 ---- 因此,閏年的規則便修正如下: 格瑞哥里法: 除了不是400的倍數的100的倍數以外,四的倍數的年份均為閏年。為了要彌補截至當時季節和日曆已產生的誤差,當時的日曆便往前挪移了10天:1582年10月4日的第二天為10月15日。英格蘭和它的帝國 (包括美國) 一直到1752年才改用格瑞哥里曆,當年的9月2日的第二天為9月14日。 (未同步採用新曆乃肇因於亨利八世和教宗的惡劣關係。) ---- 請依現行的曆法判斷所給的西元年份是平年還是閏年。 輸入說明 輸入的每一行有一個正整數 y,代表珊珊生日的西元年份。輸入的最後一行有一個 0,代表輸入的結束,這個數字請勿做任何處理。 輸出說明 對於所輸入的每個 y,要各別輸出一行。若 y 是閏年,請於該行輸出「a leap year」,否則請輸出「a normal year」。 ---- 範例輸入 #1 1992 1993 1900 2000 0 範例輸出 #1 a leap year a normal year a normal year a leap year ---- ## 解答啦靠北 ```cpp= #include <iostream> using namespace std; int main (){ int year ; while(cin>>year){ if(year==0){ break; } if(year%4){ cout<<"a normal year" << endl; }else{ if(year%400==0){ cout<<"a leap year" << endl; }else if(year%100==0){ cout<<"a normal year" << endl; }else{cout<<"a leap year" << endl;} } } return 0; } ``` --- # 結束
{"metaMigratedAt":"2023-06-16T03:21:36.819Z","metaMigratedFrom":"Content","title":"迴圈(For and While)","breaks":true,"contributors":"[{\"id\":\"71bdf46d-72d3-43b0-9f90-8e2b261abc6b\",\"add\":790,\"del\":129},{\"id\":\"5b23b090-3e7f-4d31-957a-41665bdc6388\",\"add\":6979,\"del\":2833},{\"id\":\"82f46fc6-f9dd-4e98-8fe8-19fda0dc8ba3\",\"add\":3747,\"del\":0}]"}
    548 views
   Owned this note