Pseudo-code 偽代碼 & Flow charts 流程圖 : Sum Bubble sort 1 Prime factors of Magic square Total & Average Bubble sort 2 Factors of Zodiac (simple) Quadratic equation Train fare 1+2+...+n <= limit Calculator Digital Clock Animal of the year Square numbers Word Count 三角形測試 Decimal to Binary Largest numbers Skip Comments HKCEE 198x MTR fare Smallest & largest Greetings Middle number Leap year test Trim spaces GCD/LCM Odd and Even Linear search Euclidean Algorithm Encoding / Decoding Binary search Admission Fee ASCII table a,b,c? 8 5 3 a,b,c? 7 3 10 a,b,c? 5 8 8 a,b,c? 4 0 2 a,b,c? 3 1 3 開始 輸入 a,b,c a,b,c? 5 9 1 a,b,c? 4 6 3 a,b,c? 5 5 0 a,b,c? 7 2 9 a,b,c? 1 3 2 a>b? x=b c b=b+5 x>a? b>c? c=b 3 b=c 3 x=c b a=b c b>c? b>5? x=b 2 x=c 2 x=c 4 x=x+3 結束 a,b,c,x 的最終值是什麼? p.1
Sum sum = 0 as long as sum < 10 do input x sum = sum + x end do output sum inputs: 1 2 3 4 5 output: inputs: 2 3 4 5 output: Total & Average Total = 0, Avg = 0, n = 0 IPUT x WHILE x > 0 DO Total = Total + x n = n+1 IPUT x ED WHILE IF n>0 THE Avg = Total / n OUTPUT Avg inputs: 1 2 3 4 0 output: inputs: 0 1 2 3 4 output: inputs: 1 2 3-4 0 output: Quadratic equation IPUT A,B,C D=B 2-4AC IF D=0 THE X = -B/2A OUTPUT X ED IF D<0 THE OUTPUT O REAL ROOT ED IF D>0 THE B D x1 2A B D x2 2A OUTPUT x1,x2 ED sum = 0 只要 sum<10 進行輸入 x sum = sum + x 輸出 sum Total = 0, Avg = 0, n = 0 輸入 x 只要 x > 0 進行 Total = Total + x n = n+1 輸入 x 如果 n>0 則 Avg = Total / n 輸出 Avg 輸入 A,B,C D=B 2-4AC 若果 D=0 則 X = -B/2A 輸出 X 若果 D<0 則 輸出 " 沒有根 " 若果 D>0 則 B D x1 2A B D x2 2A 輸出 x1,x2 p.2
Digital Clock IPUT HH,MM,SS WHILE HH<24 DO OUTPUT HH,MM,SS SS = SS+1 輸入 HH,MM,SS 只要 HH<24 進行輸出 HH,MM,SS SS = SS+1 IF SS=60 THE MM = MM+1 SS = 0 EDIF IF MM=60 THE HH = HH+1 MM = 0 EDIF IF HH=24 THE HH = 0 ED WHILE 若果 SS=60 則 MM = MM+1 及 SS = 0 若果 MM=60 則 HH = HH+1 及 MM = 0 若果 HH=24 則 HH = 0 試把下列 算法 (Algorithm) 改寫為 C 程式 ( 三角形測試 ) 步驟 : (1) 要求使用者輸入三角形各邊長, 並分別儲存在變量 A, B 及 C 內 ; 假設 A B C (i) (2) 儲存 C 2 的數值在變量 S1 內 C (3) 儲存 A 2 + B 2 的數值在變量 S2 內 A B (4) 跟據下表, 輸出不同的結果 條件 輸出 i 若 C 大於或等於 A 及 B 之和 not a (triangle) ( 不是三角形 ) (ii) A B C 否則 ii 若 C 2 iii 若 C 2 iv 若 C 2 小於 A 2 及 B 2 之和 an acute ( 銳角三角形 ) 等於 A 2 及 B 2 之和 a right-angled ( 直角三角形 ) 大於 A 2 及 B 2 之和 an obtuse ( 鈍角三角形 ) (iii) A B C (iv) A C B p.3
HKCEE 1991 count = 1 score = 0 R = 7 WHILE count 5 IPUT x IF(abs(R-x)<3) THE score = score + 2 ELSE score = score - 1 count = count+1 ED WHILE OUTPUT score 輸入數值 input values: 2, 5, 8, 0, 10 count = 1 score = 0 R = 7 只要 count 5 便進行輸入 x 如果 (abs(r-x)<3) 則 score = score + 2 否則 score = score - 1 count = count+1 輸出 score Greetings Time Message 00:00 Mid-night 午夜 00:01-11:59 Morning 早晨 12:00 oon 正午 12:01-17:59 Afternoon 下午 18:00-23:59 Evening 晚上 sum = 1+2+3+...+n <= limit IPUT limit sum = 0 n = 1 WHILE sum+n <= limit DO OUTPUT n sum = sum+n n = n+1 ED WHILE OUTPUT sum Trim spaces: inputs: abcd pqrxyz outputs: abcd pqrxyz 輸入 limit sum = 0 n = 1 只要 sum+n <= limit 便進行輸出 n sum = sum+n n = n+1 輸出 sum previous current previous char current char (c) space non-space space --- output c non-space output c output c inword p.4
Decimal to Binary: simple version: reversed binary IPUT dec WHILE dec>0 DO r = dec % 2 dec = dec / 2 OUTPUT r ED WHILE final version s = ULL IPUT dec WHILE dec>0 DO r = dec % 2 dec = dec / 2 attach r to the front of s ED WHILE OUTPUT s MTR Fare IPUT age, fr_stn, to_stn fare = (to_stn-fr_stn)*2 IF fare<5 THE fare = 5 IF age<12 THE fare = fare / 2 IF age>60 THE fare = fare / 2 OUTPUT fare age = 999 只要 age>0 便進行輸入 age, fr_stn, to_stn fare = (to_stn-fr_stn)*2 如果 fare<5 則 fare = 5 如果 age<12 則 fare = fare / 2 如果 age>60 則 fare = fare / 2 輸出 fare Find the middle number: IPUT 輸入 X,,Z IF 如果 X> THE 則 IF X>Z THE IF >Z THE OUTPUT ELSE OUTPUT Z ELSE 否則 OUTPUT X ED IF ELSE IF >Z THE IF X>Z THE OUTPUT X ELSE OUTPUT Z ELSE OUTPUT B: x % y remainder of x/y ED IF ED IF 輸入 x,y,z 如果 x>y 則如果 x>z 則如果 y>z 則輸出 y 否則輸出 z 否則輸出 x 否則如果 y>z 則如果 x>z 則輸出 x 否則輸出 z 否則輸出 y p.5
GCD/HCF (Simple & Straight forward) IPUT two positive integers (A & B) IF A<B THE swap A & B GCD=0 n=b WHILE n>1 AD GCD<1 DO IF A & B are both divisible by n THE GCD=n ELSE n=n-1 ED DO OUTPUT GCD 輸入兩整數 (A & B) 如果 A<B 則對換 A & B GCD=0 n=b 只要 n>1 及 GCD<1 便進行如果 A & B 可被 n 整除則 GCD=n 否則 n=n-1 輸出 GCD LCM (Simple & Straight forward) IPUT two positive integers (A & B) IF A<B THE swap A & B LCM=0 輸入兩整數 (A & B) 如果 A<B 則對換 A & B LCM=0 n=a // n=max(a,b) n=a WHILE n<axb AD LCM<1 DO IF n is divisible by both A & B THE LCM=n ELSE n=n+1 ED DO OUTPUT LCM 只要 n<axb 及 LCM<1 便進行如果 n 可被 A & B 整除則 LCM=n 否則 n=n+1 輸出 LCM LCM(A,B) GCD(A,B) = A B Euclidean Algorithm: Repeat Find C = A%B (remainder of A divided by B) Until C=0 B will be the HCF(A,B) A B C=A%B 1 20 12 8 20%12=8 2 12 8 4 12%8=4 3 8 4 0 8%4=0 HCF = 4 p.6
GCD Euclidean Algorithm 歐基里德算法/ 輾轉相除法 計算 A 和 B 的最大公因數 (GCD/HCF) 要求使用者輸入兩個整數 (A 和 B) 若 A 的值少於 B 的值, 則換轉 (Swap) A 和 B 的值 只要 B 的值大於 0 便進行把 A 除 B 的餘數 賦予 C 把 B 的值賦予 A 把 C 的值賦予 B 把 A 的值賦予 HCF 輸出 HCF 假設 Admission Fee ($100) 輸入 dow, time, age fee = 100 如果 (dow=0 或 dow=6) 則 fee = fee * 1.1 否則如果 (time<14) 則 fee = fee * 0.9 Prime factors 質因數 of n: IPUT n d=2 WHILE n>1 DO IF n is divisible by d THE OUTPUT d n = n/d ELSE d = d+1 ED IF ED WHILE Factors of n 因數 : IPUT n d=2 WHILE d<=n DO IF n is divisible by d THE OUTPUT d d = d+1 ED WHILE (i) "Swap" 是一個預定處理程序 (ii) X % 的結果是 X 除 的餘數 如果 (age>60) 則 fee = fee * 0.5 否則如果 (age<12 或學生 ) 如果 (dow=0 或 dow=6) 則 fee = fee * 0.8 否則 fee = fee * 0.5 輸出 fee 輸入 n d=2 只要 n>1 進行如果 n 可被 d 整除則輸出 d n = n/d 否則 d = d+1 輸入 n d=2 只要 d<=n 進行如果 n 可被 d 整除則輸出 d d = d+1 p.7
Encode / Decode (ASCII code of 'A'=65) 編碼 / 解碼 65 70 75 80 85 90 A B C D E F G H I J K L M O P Q R S T U V W X Z Q R S T U V W X Z A B C D E F G H I J K L M O P Encode 編碼 A(65) Q(81) B(66) R(82) I(73) (89) J(74) Z(90) K(75) A(65) L(76) B(66) (89) O(79) Z(90) P(80) Encode 編碼 IPUT alphabet c 輸入字母 c n = 16 + ASCII code of c n = 字母 c 的 ASCII 碼 IF n>90 THE n=n-26 如果 n<75 c = the n'th char in ASCII table 則 n=n+16 OUTPUT alphabet c 否則 n=n-10 c = ASCII 編碼表內第 n 個字符 輸出字母 c Decode 解碼輸入字母 c n = 字母 c 的 ASCII 碼 - 16 如果 n<65 則 n=n+10 c = ASCII 編碼表內第 n 個字符輸出字母 c Linear Search, Binary Search, Bubble Sort: Train Fare: Word Count: Skip Comments: Ping-Pong Rebound: x++,y++ (0,20) (80,0) To determine if year (yy) is a Leap ear or not: yy = 999 只要 yy>0 便進行輸入 yy 如果 yy%400=0 則 Leapear="" 否則如果 yy%4=0 則如果 yy%100 0 則 Leapear="" 否則 Leapear="" 否則 Leapear="" 輸出 Leapear p.8