--- lang: zh-TW --- ###### tags: `Deep Learning` <font size=6>**APCS作業第二周**</font> :::info [Toc] ::: --- # 上課內容及筆記 # 11/5 ## 系統建置 >我們使用了virtual box建構了之後上課及之後考試時使環境 >我發現virtual box的容量有那麼一點大(裝了一些時間) >--- ## 觀摩影片 * 簡單的先介紹c語言 {%youtube LCzATsQ2onI%} >--- ## 影片筆記 ### 電腦組成 * ![image](https://hackmd.io/_uploads/HJGl6ewVT.png) * [電腦構造](https://blog.51cto.com/u_15064656/2601793) * ![image](https://hackmd.io/_uploads/B1suCxwN6.png) ### C語言標準 * ![image](https://hackmd.io/_uploads/rkeJkWwN6.png) * 標準很重要:確認你所寫的程式標準哪一版 * 可以向前相容,但C89並不會認是C11 * 考試環境 * ![image](https://hackmd.io/_uploads/HkECybwNT.png) ### C語言編輯流程 * ![image](https://hackmd.io/_uploads/rJIdgZwEp.png) * ![image](https://hackmd.io/_uploads/rJWigZPEp.png) ### C語言編輯器 * ![image](https://hackmd.io/_uploads/SynmWWDEp.png) * 編輯器的差異 * 效率 * 程式碼產生機器碼的效率 * 機器碼執行的效率 * 相容性 * 標準外的自訂擴充功能 * 行為一致性 * 實作定義之行為 * 不同版本或使用不同設定的同款編譯器也可能產生不一樣的行為 ## 11/19 ## 系統建置 >我們使用了virtual box建構了之後上課及之後考試時使環境 >我發現virtual box的容量有那麼一點大(裝了一些時間) >--- ## 觀摩影片 * 簡單的先介紹c語言 {%youtube LCzATsQ2onI%} >--- ## 影片筆記 * 開始: 1 - [1.1 - 字元的跳脫](https://youtu.be/Nv4bqkMjc68?si=mNIR8uXkxZDfm5_8) * 結束: 2 - [1.3 - 求兩個數字的和練習](https://youtu.be/y_VioPAikxg?si=lZ__WjqHoc3SUVVw) ### 1.1字元的跳脫 * ![image](https://hackmd.io/_uploads/S1NEDSD4T.png) * 雙引號中再加一個雙引號 例如: ```c= "hello\"c\"happy" =hello"c"happy ``` * 想印出\n 打出\\\n ### 1.2 簡單的文字圖形練習 * 簡單明瞭比較重要,但要記得資源是有限的,效率可到最後要提交答案後再進行程式碼簡化。 * ![image](https://hackmd.io/_uploads/HkRjnUw4a.png) * 最佳解 * ![image](https://hackmd.io/_uploads/Hyt63LDEa.png) ### 1.3 求兩個數字的和練習 * ![image](https://hackmd.io/_uploads/rJ8I1IDET.png) * scanf代表從鍵盤讀 * %d表示十進位整數 * &表示從記憶體中取位置 # 作業 影片進度 * 看到 【C 語言入門】1 - 跟我的第一個程式說 Hello * 學習所有程式語言的第一個練習題 * 請熟悉現在寫程式的環境, * 輸出指定的字串。請輸出 "Hey, Kitty" * HackMD 上課筆記 * 本次課堂的HackMD筆記 --- # 我的作業進度 ## 11/5 - [x] 觀看影片1-6 - 編譯器效率、相容性、行為一致性的差異 - 實作定義(允許不同編譯有些許彈性) - [x] 觀看影片2 - c語言製作過程 - 常見的整合開發軟件 - [x] 觀看影片2-1 - 介紹及下載Code::Blocks - [x] 觀看影片2-2 - [x] 觀看影片2-3 - 如何執行及改變字型大小 - [x] 觀看影片0.01 - 檢查錯誤 - [x] 跟我的第一個城市說Hello # 11/19 --- # 程式作業 ## 建立第一個程式(Print) * 輸出指定的字串。請輸出 “Hey, Kitty” ```c= #include <stdio.h> int main(){ printf("Hey, Kitty"); return 0; } ``` ![S__4907012](https://hackmd.io/_uploads/S1c00QP46.jpg) ## 跳脫字元 * 課堂練習 * ![image](https://hackmd.io/_uploads/SJgLerwVT.png) ```c= #include <stdio.h> int main() { printf("Hello\n world! "); return 0; } ``` * ![image](https://hackmd.io/_uploads/BknseSP46.png) ```c= #include <stdio.h> int main() { printf("Hello \"c\" world! "); return 0; } ``` * ![image](https://hackmd.io/_uploads/BkEMbrD4p.png) ```c= #include <stdio.h> int main() { printf("Hello \\n world! "); return 0; } ``` * 作業一 * ![image](https://hackmd.io/_uploads/HyV4-rwEp.png) ```c= #include <stdio.h> int main() { printf("H H A\n"); printf("H H A A\n"); printf("HHHHH A A\n"); printf("H H AAAAA\n"); printf("H H A A\n"); return 0; } ``` * 作業二 * ![image](https://hackmd.io/_uploads/S1mg1IPET.png) ```c= int main() { int integer1; int integer2; int integer3; int sum; printf("Please enter the first integer: "); scanf("%d", &integer1); printf("Please enter the second integer: "); scanf("%d", &integer2); printf("Please enter the third integer: "); scanf("%d", &integer3); sum=integer1+integer2+integer3; printf("Sum is %d.\n", sum); return 0; } ``` # 12/3 ## sum的結合 * ![image](https://hackmd.io/_uploads/rJ8I1IDET.png) * ![image](https://hackmd.io/_uploads/SyjpQotB6.png) * 相較於上方的程式,下方的程式會使用更少的空間,使用較少的記憶體,但缺點可能是需花費較多的時間來執行。 * =是一個資料夾裝著檔案的概念,等於左邊的東西會等於右邊的東西。 * ![image](https://hackmd.io/_uploads/HJpo4jtS6.png) * 對初學者來說,前兩項較為重要。 * 效率:考試不可能使用一大堆的定義,需要透過簡化提升效率 ## 符點數 * 能用來表示小數的數 * 優點:表示的範圍廣 缺點:表示只是約略值,可能不符預期、較複雜、效率較差 ### 有效數字 * 位數越多越準確 * 例:1.2345取三位有效,值為1.23 ## 數值交換 ```c= #include <stdio.h> int main() { int integer1, integer2; printf("Please enter the first integer: "); scanf("%d", &integer1); printf("Please enter the second integer: "); scanf("%d", &integer2); int temp = integer1; integer1 = integer2; integer2 = temp; printf("integer1: %d\n", integer1); printf("integer2: %d\n", integer2); return 0; } ``` * 上述是利用增加一個變數使兩者能達到交換的作用 * temp是暫存變數 ### 資料儲存 * ![image](https://hackmd.io/_uploads/r15bj3tBp.png) * 任何資料都能以0、1表示 ### 位元組(byte) * 電腦記憶儲存空間是以位元組為單位 * 位元組由許多位元所組成 * ![image](https://hackmd.io/_uploads/r1Zqi2YS6.png) * 位元組中的*表示實作定義(老師口語的重點記得回去看影片用,把重點用口述的方式筆記起來) * sizeof:看所占記憶體空間大小 * ![image](https://hackmd.io/_uploads/Hk5eChYra.png) * 這些有時間要記得記(sizeof很重要記得補充) ![image](https://hackmd.io/_uploads/BJN8KTYHp.png) ## 作業 ### 作業一: * 依照[影片數值交換](https://www.youtube.com/watch?v=bhO9e24gkJk&list=PLY_qIufNHc293YnIjVeEwNDuqGo8y2Emx&index=19)的模式,隨意輸入三個整數數值至變數值(a,b,c)後,後面請進行三個變數值的交換,例如將變數a,b,c得整數值放到b,c,a,並於螢幕上輸出(printf) ```c= #include <stdio.h> int main() { int integerA, integerB, integerC; printf("Please enter the first integer: "); scanf("%d", &integerA); printf("Please enter the second integer: "); scanf("%d", &integerB); printf("Please enter the first integer: "); scanf("%d", &integerC); int temp = integerA; integerA = integerB; integerB = integerC; integerC = temp; printf("integer1: %d\n", integerA); printf("integer2: %d\n", integerB); printf("integer2: %d\n", integerC); return 0; } ``` ### 作業二: * 我有以下資料, * studentID: 15; * studentAge: 23; * studentFee: 75.25 * studentGrade: 'B'; * 請寫一段程式碼,輸出和下圖一樣的結果。 * Hint: 字串要用%c, 浮點數用%f * ![image](https://hackmd.io/_uploads/HkOUl6FBp.png) ```c= #include <stdio.h> int main() { int student; int studentAge; float studentfee; char studentGrade; student = 15; studentAge = 23; studentfee = 75.25; studentGrade = 'B'; printf("%d\n",student); printf("%d\n",studentAge); printf("%f\n",studentfee); printf("%c\n",studentGrade); return 0; } ``` # 12/9 ## 12/9 模擬測驗 ### 觀念題 1. (一)使用函數printf()輸出字元時必须使用以下哪一種格式?(A) %s(B)%c$(C)$%d(D)%f B 2. (二)底下哪一個不是C語言的資料型態?$(A)int(B)char(C)float(D)image$ D 3. (三)以下選項中,合法的數值型常量是$(A)092(B)A”(C)3.1415(D)0xDH$ C 4. (四)若有定義:char c ;int d;程序運行時輸入:c=1,d=2,能把字元1輸入給變數C·整敷2輸入給變敷d的輸入語句是 * ![](https://hackmd.io/_uploads/r1v4psxI6.png) C 5. (五)程式執行時·程式中的雙数值是存放在$(A)記憶體(B)硬碟(C)输出入装置(D)匯流排$ A 6. (六)程式執行過程中,若變数發生溢位情形,其主要原因為何? * $(A)$以有限数目的位元储存變數值 * $(B)$電壓不稳定 * $(C)$作業系統與程式不甚相容 * $(D)$變數過多導致编譯器無法完全處理 A ### 實作題 1. (一)隨意輸入兩個變數字串值,完成交換後輸出在螢幕 * 比如1:輸入字串 "a"和"b"傅值於A和B兩個變數, 將兩個變數值交換後,A變數值為b,而B變數為a,並輸出於螢幕 ```c= #include <stdio.h> int main() { char A, B; printf("Please enter the first integer: "); scanf("%c", &integerA); printf("Please enter the second integer: "); scanf("%c", &integerB); char temp = integerA; integerA = integerB; integerB = temp; printf("integer1: %c\n", integerA); printf("integer2: %c\n", integerB); return 0; } ``` 2. (二)學習所有程式語言的第一個練習題,請寫一個程式,可以讀入指定的字串,並且輸出指定的字串在螢幕上。 * 比如1:輸入字串 "world", 則請輸出 "hello, world" * 比如2:輸入字串 "cat", 則請輸出 "hello, cat" ``` #程式碼寫在這 ``` ## 12/11 周回家影片/筆記範圍 * (回家)練習及影片時間 * 12/18 周 (每天影片不超過30分鐘) * W一 (2.4) * 2.4 [2.4 - 整數與浮點數型別間的轉換](https://youtu.be/npifsOxKnUQ?si=7exQuJ-Rx0YXuyo0) * W二(2.5~2.8) * 2.5 [2.5 - 字元型別簡介](https://youtu.be/jp4yl8DhNKk?si=Uz8JNwwWMbo_onBn) * 2.8 [2.8 - 怎麼對 char 型別做運算](https://youtu.be/-GDtfoxLxn8?si=K7Xo6Oqt9tjY9LWO) * W三(2.9~3.1) * 2.9 [2.9 - 字元大小寫轉換練習](https://youtu.be/mFAXup3VAOI?si=zgKgChJBUWWgDwYV) * 3.1 [3.1 - 整數與浮點數的算術運算](https://youtu.be/ApiuA8JvZTE?si=tbaqORMRZMT3Pxyb) * W四(3.2~3.4) * 3.2 [3.2 - 賦值運算](https://youtu.be/ZnmNzU92i8E?si=V64_Wumkzh3Z00WZ) * 3.4 [3.4 - 關係運算](https://youtu.be/YwKbW3hZags?si=AFSSoANPEUhoweHT) * W五(3.5~3.6) * 3.5 [3.5 - 邏輯運算](https://youtu.be/ZpNnTWrIUcM?si=jz1n42rdUBPO_DLI) * 3.6 [3.6 - 運算子的優先順序](https://youtu.be/ZYhG_wA57LY?si=enwYORtYmYBluQEM) # 12/11 ### 影片 * ![image](https://hackmd.io/_uploads/rkHp-GsUT.png) * 電腦為了減少出錯,會轉型成範圍較大的型別 * ![image](https://hackmd.io/_uploads/Hy5uMMi8p.png) *  ![image](https://hackmd.io/_uploads/ByKiQfoIp.png) * 圖中的A是以整數的方式儲存 * ![image](https://hackmd.io/_uploads/ByAg4Mo8T.png) * ![image](https://hackmd.io/_uploads/r1cUNGjUT.png) * ![image](https://hackmd.io/_uploads/SJ8CVGiU6.png) * 整數可以求餘數,符點數不行 * ![image](https://hackmd.io/_uploads/r1aTSzsUT.png) * 賦值運算子是由右到左 * 賦值運算子通常較晚執行 * ![image](https://hackmd.io/_uploads/HygddMsLa.png) * ![image](https://hackmd.io/_uploads/ryQ1YGsL6.png) ### 12/16 * 為何要第一航 因為項printf與scanf已經載入到那個模組了 * int main表示程式要開始了 * 註解/* */ * 單行註解// * integer是整數 * char是字串 * getchar清除緩衝區 ### 模擬測驗 * 選擇題:0分 * 實作提:20分 * 總得分:20 #### 觀念題 1. 右側程式碼執行後輸出結果為何? * ![image](https://hackmd.io/_uploads/Sk-Kod58p.png) * (.A) 3 (.B) 4 (.C) 5 (.D) 6 b 2. 若宣告一個字元陣列$char\ str[20] =$ "Hello world!"; 該陣列$str[12]$值為何? * (.A) 未宣告 (.B) \0 (.C) ! (.D) \n b(這題我用猜的,下次上課可能需要老師講一下) #### 實作題. 1. 學習所有程式語言的第一個練習題,請寫一個程式,可以讀入指定的字串,並且輸出指定的字串在螢幕上。 * 比如1:輸入字串 "world", 則請輸出 "hello, world" * 比如2:輸入字串 "cat", 則請輸出 "hello, cat" ```c= #include <stdio.h> int main() { char A[5]; //輸入字串要用陣列方式儲存, 陣列相當於是一堆格子,宣告五個格子 scanf("%c", &A); printf("hello, "); printf("%c",A); return 0; } ``` * 這程式需要宣告個字串,那就是宣告陣列 * 宣告大小為五的陣列 2. 請寫一個程式,讀入兩個數字,並求出它們的和。 * 輸入說明: 每組輸入共一行,內含有兩個整數 a, b,以空白隔開,a, b絕對值皆小於$10^6$ * 輸出說明: 對於每組輸入,輸出該兩整數的和。 * 範例輸入 #1 * 5 10 * 範例輸出 #1 * 15 * 範例輸入 #2 * 1 2 * 範例輸出 #2 * 3 ```c= #include <stdio.h> int main() { int a,b; scanf("%d", &a); scanf( "%d", &b); printf("%d",a+b); return 0; } ``` 3. 我是電視迷:我愛看電視,更愛切頻道,可恨的是遙控器上的所有按鍵全壞光了,只剩下「下一個頻道」可以用。我的電視有 100 個頻道,編號為 0 ~ 99。給你我現在正在看的頻道,以及我要切換過去的頻道,請問我得按幾次「下一個頻道」。 * 輸入說明 * 輸入只有一行,含有兩個整數 a, b,分別代表現在的頻道及要切換過去的頻道。 * 輸出說明 * 輸出所需按「下一個頻道」的次數。 * 範例輸入 #1 * 5 10 * 範例輸出 #1 * 5 ```c= #include <stdio.h> int main() { int a,b; scanf("%d", &a); scanf( "%d", &b); printf("%d",b-a); return 0; } ``` ## 12/18 周筆記&作業內容複習 * 10分鐘:報告你自己上周所學的內容 * W一 (2.4) * 2.4 [2.4 - 整數與浮點數型別間的轉換](https://youtu.be/npifsOxKnUQ?si=7exQuJ-Rx0YXuyo0) * W二(2.5~2.8) * 2.5 [2.5 - 字元型別簡介](https://youtu.be/jp4yl8DhNKk?si=Uz8JNwwWMbo_onBn) * 2.6 字元編碼簡介 * 2.7 使用 char 字元型別 * 2.8 [2.8 - 怎麼對 char 型別做運算](https://youtu.be/-GDtfoxLxn8?si=K7Xo6Oqt9tjY9LWO) * W三(2.9~3.1) * 2.9 [2.9 - 字元大小寫轉換練習](https://youtu.be/mFAXup3VAOI?si=zgKgChJBUWWgDwYV * 3 運算與表示式 * 3.1 [3.1 - 整數與浮點數的算術運算](https://youtu.be/ApiuA8JvZTE?si=tbaqORMRZMT3Pxyb) * W四(3.2~3.4) * 3.2 [3.2 - 賦值運算](https://youtu.be/ZnmNzU92i8E?si=V64_Wumkzh3Z00WZ) * 3.3 怎麼表示是非對錯? * 3.4 [3.4 - 關係運算](https://youtu.be/YwKbW3hZags?si=AFSSoANPEUhoweHT) * W五(3.5~3.6) * 3.5 [3.5 - 邏輯運算](https://youtu.be/ZpNnTWrIUcM?si=jz1n42rdUBPO_DLI) * 3.6 [3.6 - 運算子的優先順序](https://youtu.be/ZYhG_wA57LY?si=enwYORtYmYBluQEM) ## 12/19(二) #### 2-5 * 儲存文字時,需轉變成由0、1所組成的代碼 * 如何轉變 * 給每個字元一個獨一無二的代碼 * 字元也是種[整數]型別 #### 2-6 * 字元的可能性 * 英文 * 符號 * 數字 * 加起來小於256 * 使用一位元組就可表示(256個) * 使用哪種編碼是實作定義 * ![image](https://hackmd.io/_uploads/r1SRrtePp.png) 上方圖片是ASCII1編碼 #### 2-7 * char * 字元型別 * 佔記憶體一個位元組 * 用多個char表示中字 * 如何表示 * 如何表示: * 利用單引號括住 * 字元格式符 * 使用%c ```c= #include <stdio.h> int main() { char ch='A'+1; printf("%c\n",ch); return 0; } ``` #### 2-9 * 大寫轉小寫練習 ```c= #include <stdio.h> int main() { char input,output; scanf("%c",&input); output=input+32; printf("%c\n",output); return 0; } ``` #### 3.運算與表示式 ![image](https://hackmd.io/_uploads/HJZkjJzwT.png) * 資料間有優先順序(先乘除後加減) * 運算子一次只能執行一個 * 5*2的答案會儲存為暫時物件 #### 3-1整數與浮點數的算術運算 * ![image](https://hackmd.io/_uploads/SJ8CVGiU6.png) * 運算是由左到右,先乘除後加減 * 符點數的除法不會有餘數,整數有 * 一個數除以二餘一   表示該數為奇數 #### 3-2賦值運算 * a=3是將右邊的3帶到左邊的變數 * 左方變數值得改變是一種副作用 * 副作用: * 賦值運算的結果是左方變數最後的型別跟值 * 賦值運算的運算順序是由右到左 #### 3.3 - 怎麼表示是非對錯? * 是非對錯是二分型資料(只有兩種可能) * 只需一個位元即可表示(1表真,2表假) * 表示式的真假(非0為真,0為假) #### 3.4 - 關係運算 * ![image](https://hackmd.io/_uploads/Bkc2llTDp.png) * 若結果為真,算出1 * 3==5與3=5的差別 * 3>2>1的比較是真還是假 #### 3.5 - 邏輯運算 * 關係運算子>邏輯運算子(順序較早) * 短路求值 * !的意思為[非],將結果相反 * !優先於關係運算子 #### 3.6 - 運算子的優先順序 * 算術運算優先於關係運算 * ![image](https://hackmd.io/_uploads/BJEkrlpPp.png) * &&優先於|| * !為邏輯運算子,但它是最優先的 * !由右往左 * =賦值運算最後,也是由右至左 # 新年快樂, 下周進度 * 記得看影片寫筆記 * W日 (4-4.2):28分鐘 * 4 [4 - 程式流程控制](https://youtu.be/PdooaPC7UKI?si=gcNWPOloPyr0fsmM) * 4.1 [4.1 - 有條件的執行 (if 述句)](https://youtu.be/0pmKwgMXBqg?si=ySSTYIe4dHJ83efX) * 4.2 [4.2 - 猜數字 (使用 if 述句)](https://youtu.be/WmO5HuDjS68?si=jxuMbdqUxyag4j18) * W二(4.3): 21 分鐘 * 4.3 [4.3 - 滿額折扣計算練習 (使用 if 述句)](https://youtu.be/AwJBDyQlbcw?si=tr3HA4TO68SkxCcn) * W三(4.4~4.6): 29分鐘 * 4.4 [4.4 - 正三角形判斷練習 (使用 if 述句)](https://youtu.be/t91H7TCPcXg?si=8lMWk-btgCO5lZw_) * 4.5 [4.5 - 等腰三角形判斷練習 (使用 if 述句)](https://youtu.be/yf-FhivhFdo?si=EaE4GxcADQCEKQG8) * 4.6 [4.6 - 直角三角形的判斷練習 (使用 if 述句)](https://youtu.be/NkSQRnikOVs?si=8UoVbWdfsCOKMRkw) * W四(4.7~4.8): 20分鐘 * 4.7 [4.7 - 三角形種類判斷練習](https://youtu.be/mFQcqb2Zbik?si=IkOlsjhuo5aFKS_U) * 4.8 [4.8 - 三角形種類判斷的條件簡化](https://youtu.be/4TgJtNamSv0?si=MsLYM405gI-p0YCm) * W五(5.1~5.2): 22分鐘 * 5.1 [5.1 - 對兩個變數求最大值 (使用 if 述句)](https://youtu.be/BQmGFI3Lr4k?si=dPX_-3LncV7PZAX5) * 5.2 [5.2 - 對三個變數求最大值的練習 (使用 if 述句)](https://youtu.be/mibcvQOfmIM?si=rHO1McY27ACc_C1j) ## 1/1 #### 4-0流程控制 * 流程控制? * 原本是無條件執行一次 * 有條件的執行(if) * 無條件地重複執行(無窮迴圈) * 有條件地重複執行(While) * 計次型重複執行(for) * 選擇性執行(選擇題)(switch) #### 4-1 if語法  if: * 標準式: * if(表示式){程式片段} * 若表示式成立,執行程式片段 ![image](https://hackmd.io/_uploads/HyTjzABOT.png) #### 4-2 ~~~c= #include <stdio.h> int main() { int answer=4; int guess; printf("please enter your guess:"); scanf("%d",&guess); if(guess>answer) { printf("Too large!\n"); } if(guess<answer) { printf("Too small!\n"); } if(guess==answer) { printf("Correct!\n"); } return 0; } ~~~ * 猜數字 #### 4-3滿額折扣計算 ~~~c= #include <stdio.h> int main() { int number,total,A; printf("please enter the number of customers:"); scanf("%d",&number); A=300*number; if (A<3000) { total=A; } if (A>3000) { total=A*0.8; } printf("total:%d\n",total); return 0; } ~~~ #### 4-4正三角判別 ~~~c= #include <stdio.h> int main() { int side1,side2,side3; printf("please enter the lengths:"); scanf("%d%d%d",&side1,&side2,&side3); if (side1==side2) { if(side2==side3){ printf("Regular triangle\n"); } } return 0; } ~~~ #### 4-5等腰三角形 ~~~c= #include <stdio.h> int main() { int side1,side2,side3; printf("please enter the lengths:"); scanf("%d%d%d",&side1,&side2,&side3); if (side1==side2||side1==side3||side2==side3) { printf("Isosceles triangle\n"); } return 0; } ~~~ #### 4-6直角三角形 ~~~c= #include <stdio.h> int main() { int side1,side2,side3; printf("please enter the lengths:"); scanf("%d%d%d",&side1,&side2,&side3); if (side1*side1+side2*side2==side3*side3|| side1*side1+side3*side3==side2*side2|| side2*side2+side3*side3==side1*side1) { printf("Rectangular triangle\n"); } return 0; } ~~~ #### 4-7三種三角形判別 ~~~c= #include <stdio.h> int main() { int side1,side2,side3; printf("please enter the lengths:"); scanf("%d%d%d",&side1,&side2,&side3); if (side1==side2&&side2==side3) { printf("Regular triangle\n"); } if (side1==side2||side1==side3||side2==side3) { printf("Isosceles triangle\n"); } if (side1*side1+side2*side2==side3*side3|| side1*side1+side3*side3==side2*side2|| side2*side2+side3*side3==side1*side1) { printf("Rectangular triangle\n"); } return 0; } ~~~ #### 4-8三角形簡化 ![image](https://hackmd.io/_uploads/r1Wr2hI_6.png) * 利用假設及觀察,簡化程式的運算 #### 5-1兩變數求最大值 ~~~c= #include <stdio.h> int main() { int A, B, max; printf("Please enter the first integer: "); scanf("%d", &A); printf("Please enter the second integer: "); scanf("%d", &B); if(A>=B) { max=A; } if(A<B) { max=B; } printf("The maxinum is %d.\n",max); return 0; } ~~~ ~~~c= #include <stdio.h> int main() { int A, B, max; printf("Please enter the first integer: "); scanf("%d", &A); printf("Please enter the second integer: "); scanf("%d", &B); max=A; if(A<B) { max=B; } printf("The maxinum is %d.\n",max); return 0; } ~~~ #### 5-2三個變數求最大值 ~~~c= #include <stdio.h> int main() { int A, B,C, max; printf("Please enter the three integer: "); scanf("%d%d%d", &A,&B,&C); if(A>=B&& A>=C) { max=A; } if(B>A&&B>C) { max=B; } if(C>A&&C>B) { max=C; } printf("The maxinum is %d.\n",max); return 0; } ~~~ ~~~c= #include <stdio.h> int main() { int A, B,C, max; printf("Please enter the three integer: "); scanf("%d%d%d", &A,&B,&C); max=A; if(B>max) { max=B; } if(C>max) { max=C; } printf("The maxinum is %d.\n",max); return 0; } ~~~ #### 5-3對四個變數求最大值的練習 (使用 if 述句) ~~~c= #include <stdio.h> int main() { int a,b,c,d, max; printf("Please enter the four integer: "); scanf("%d%d%d%d", &a,&b,&c,&d); max=a; if (b > max){ max=b; } if (c > max){ max=c; } if (d > max){ max=d; } printf("The maxinum is %d.\n",max); return 0; } ~~~ #### 5-4對多個變數求最小值的練習 (使用 if 述句) ~~~c= #include <stdio.h> int main() { int a,b,c, min; printf("Please enter the four integer: "); scanf("%d%d%d", &a,&b,&c); min=a; if (b < min){ min=b; } if (c < min){ min=c; } printf("The mininum is %d.\n",min); return 0; } ~~~ #### 5-5對三個變數求中位數的練習 (使用 if 述句) ~~~c= #include <stdio.h> int main() { int a,b,c, med; printf("Please enter the three integer: "); scanf("%d%d%d", &a,&b,&c); med=a; if (a <=b && b <=c ||c <= b && b <= a){ med=b; } if (a <=c && c <=b ||b <= c && c <= a){ med=c; } printf("The median is %d.\n",med); return 0; } ~~~ * 在做中位數時,相對最大最小值較難簡化 #### 6 對多個變數排序 (使用 if 述句) * 排列的用途: * 處理後產生具有某種順序的資料序列 * 找最大值 * 找最小值 * 找中位數 * 多個變數的排序練習 * 依大小(利用數值交換) * 2數的 ~~~c= #include <stdio.h> int main() { int a, b, t; scanf("%d%d", &a, &b); printf("Before: %d %d\n",a, b); if (a > b){ t=a; a=b; b=t; } printf("After: %d %d\n", a, b); return 0; } ~~~ #### 6-3 ![image](https://hackmd.io/_uploads/ryTaD3Xq6.png) * 三個變數比大小時,將會有這三種可能 # 01/20 * 10分鐘:報告你自己上周所學的內容 * W一 (5.1-5.2): 22分鐘 * [5.1 - 對兩個變數求最大值 (使用 if 述句)](https://youtu.be/BQmGFI3Lr4k?si=dPX_-3LncV7PZAX5) * [5.2 - 對三個變數求最大值的練習 (使用 if 述句)](https://youtu.be/mibcvQOfmIM?si=rHO1McY27ACc_C1j) * W二(5.3~5.5): 25 分鐘 * [5.3 - 對四個變數求最大值的練習 (使用 if 述句)](https://youtu.be/WumkwgifoTs?si=le8KbXxs32GDum5E) * [5.4 - 對多個變數求最小值的練習 (使用 if 述句)](https://youtu.be/WFWbPBNhTdI?si=Rl_Wtbju1qyGXDkx) * [5.5 - 對三個變數求中位數的練習 (使用 if 述句)](https://youtu.be/wp_KmvLDaGE?si=N0_emJp1C3Pnfzrx) * W三(6~6.1): 26分鐘 * [6 - 對多個變數排序 (使用 if 述句)](https://youtu.be/xTMobOcVw_I?si=1DSF0p-gCCN3cMY1) * [6.1 - 對兩個變數依照大小排序練習 (使用 if 述句)](https://youtu.be/YRRiCyCGO1o?si=40Os_6QA0qbnWNwZ) * W四(6.2~6.3): 29分鐘 * [6.2 - 三個變數的數值交換 (使用 if 述句)](https://youtu.be/hV0PiiKel6w?si=DtpWJ_kR5H_rL6o0) * [6.3 - 對三個變數依照大小排序練習 (使用 if 述句)](https://youtu.be/ZetMcX7anqw?si=uwMdz5m9L2n7RiE5) * W五(6.4~6.6): 34分鐘 * [6.4 - 用兩個變數的數值交換對三個變數做排序 [上] (使用 if 述句)](https://youtu.be/NyhgzqQoSNA?si=ZGvDWhjICV66ZLZM) * [6.5 - 用兩個變數的數值交換對三個變數做排序 (使用 if 述句) [下]](https://youtu.be/gc77DX9iGC8?si=Mq9uz1eifXfF5n7_) * [6.6 - 用排序簡化三角形判斷條件 (使用 if 述句)](https://youtu.be/IW7iQICoePg?si=Xwt-oZ3pnbeNEPg7)