發表文章

Code Maintenance & Programming Rules

 This guide outlines essential best practices spanning code style, architectural design, debugging, testing, performance, and portability—all aimed at reducing the long-term cognitive load of code maintenance. 🎨 1. Style Code is written for humans to read, and only incidentally for computers to execute. Variable Naming : Use descriptive names for global variables, and short names for local variables. Precision and Consistency : Use active names for functions (e.g., calculateTotal ). Above all, keep your coding style consistent throughout the project. Structure & Expressions : Use a consistent indentation and brace ( {} ) style to show program structure visually. Use the natural form for expressions. Use parentheses to make the semantics unambiguous. Break up overly complex expressions to keep them clear. Side Effects & Macros : Beware of functions with side effects. Avoid function-like macros; if unavoidable, parenthesize the macro body and arguments carefully. Magic Numbe...

APCS 程式識讀與實作題全方位解題技巧與備考攻略

在面對 APCS(大學程式設計先修檢測)以及各類程式線上評測系統(如 ZeroJudge 高中生解題系統、LeetCode、UVa、HackerRank 等)時,許多考生常卡在「寫不出程式」或「時間不夠」的窘境。本文將從 理解、分析、工具、實戰 四個維度,系統化解析如何有效提升 APCS 的程式識讀題與實作題分數。 一、 策略一:看懂題目,培養關鍵字敏銳度 「看不懂題目,就等於準備交白卷。」 > 這是許多初學者的痛點。APCS 的題目敘述往往結合了生活情境或數學模型,字數繁多。要克服這個障礙,秘訣在於 「多讀題、少動手」 的階段性訓練。 1. 如何訓練「看懂題目」的能力? 刻意練習「只讀不解」 :挑選 20 到 30 題歷屆試題或 ZeroJudge 基礎題, 限制自己只看題目與範例輸入輸出,先不寫程式 。嘗試在 3 分鐘內用自己的話解釋:「這題要我輸入什麼?經過什麼處理?最後輸出什麼?」 熟悉標準出題結構 :APCS 實作題通常包含四大區塊: 問題描述 (情境與規則) 輸入說明 (資料範圍、型態、資料筆數) 輸出說明 (格式要求,如空格、換行) 範例輸入/輸出 (驗證理解的最強工具) 2. 題型大解密 程式識讀題(選擇題) : 主要評量程式邏輯追蹤。核心考點包括 遞迴函式(Recursion) 、迴圈控制、條件判斷、二維陣列、以及基礎指標運算。考生必須具備「肉眼模擬 CPU」的能力。 實作題(程式撰寫) : 每場考試固定 4 題,難度由易入難(第一題通常為基本邏輯與陣列操作;第四題多為複雜圖論、動態規劃或高階演算法)。 實作題採「部分得分制」 ,每題都有明確的測資範圍(如 N <= 100  得 40 分, N <= 100000  得 100 分),作答時應優先搶下所有題目的基本分數。 二、 策略二:分析題目,用紙筆解構演算法 當讀懂題目後,不要立刻敲鍵盤,盲目寫程式只會讓邏輯陷入泥沼。 1. 用紙筆追蹤規律 面對複雜的觀念題或卡住的實作題, 紙與筆是你最好的武器 。 觀念題 :在紙上畫出變數的變更表格(Trace Table)或是遞迴樹(Recursion Tree),一步步記錄每行程式碼執行後的結果,通常就能看出數列或邏輯的「規律」。 實作題 :利用題目提供的「範例輸入」,手動模擬一次運算流程,確認自己的想法與「範例輸出...

APCS實作題2025年10月第1題彗星撞擊參考解法

圖片
題目連結 https://zerojudge.tw/ShowProblem?problemid=r488 。 解題思維: 此題用兩個二維陣列分別用來存放每個座標上的恐龍數量(dino[][])與地面高度(height[][]),並檢查撞擊範圍是否超出地圖範圍。 在範圍內 超出範圍 C++ 程式碼 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 #include <bits/stdc++.h> using namespace std ; int main () { int r,c,d,k; //c是行,r是列 cin >> r >> c >> d >> k; int dino[ 105 ][ 105 ] = { 0 },height[ 105 ][ 105 ] = { 0 }; // 讀取恐龍座標 while (k -- ){ int x,y; cin >> x >> y; dino[x][y] ++ ; // 此座標增加一隻恐龍 } // 設定地圖地面高度 for ( int i = 0 ;i < r;i ++ ){ for ( int j = 0 ;j < c;j ++ ){ height[i][j] = d; } } int m; // 撞擊次數 cin >> m; while (m -- ){ ...

Sorting Algorithm 排序演算法介紹

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much. 在本部落格的文章: Python 排序演算法範例 ( Sorting Algorithms in Python ) 有介紹一些常見的四種排序演算法:插入排序、選擇排序、合併排序、氣泡排序。除了這四種之外還有其他的嗎? 當然有!!! 例如:謝耳排序、快速排序、堆積排序、基數排序、雞尾酒排序等。那排序演算法到底在做什麼?!又為什麼會有這麼多種的演算法呢?! 排序是要將東西依照某種規則放置,例如將一個數列由小排到大,或是將容器依照體積由大排到小。在電腦科學裡,排序是將元素依照數值大小或是字典順序來排列。但因為考量到排序的速度(計算量),於是科學家們想出了各式各樣的排序演算法,有些排序演算法適用於資料量小的情境,有些排序演算法適用於資料量大的情境。對於排序演算法的選擇可從排序方法是用 比較式 的、還是 非比較式 的,以及時間複雜度與實際資料分布的狀況來選擇。 在  https://github.com/TheAlgorithms 上有以不同的程式語言實作各式各樣的演算法,例如 Python 排序演算法 、 Java排序演算法 、 C語言排序演算法 、等。此外若想看一些常見演算法的動畫,可參考【 會動的演算法 】一書的動畫網址: https://www.flag.com.tw/activity/F2708/exercise/books/

LeetCode 解題練習:Plus One

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much. 題目原文描述  https://leetcode.com/problems/plus-one/ 中文描述 給定一個用陣列 digits 表示大整數 large integer,digits[i] 代表大整數的第 i 位數之值。例如 12345 會以 digits = [1, 2, 3, 4, 5] 來表示。請將 digits 的數值加一。 範例一: 輸入 digits = [9,9,9,9]  輸出  [1, 0, 0, 0, 0] 說明:整數 9999  加一後為 10000。 範例二: 輸入 digits = [2, 2, 3, 4, 5]  輸出  [2, 2, 3, 4, 6] 說明:整數 22345  加一後為 22346。 解法: 若目前 digits[i] 為 9 ,將 digits[i] 設定為 0;否則 digits[i] 加一並回傳 digits。若每位數都是9,在最左邊補1。 Python Code class Solution :     def plusOne ( self , digits : List[ int ]) -> List[ int ]:         for i in range ( len (digits) - 1 , - 1 , - 1 ): # 從最右邊位置開始判斷             if digits[i] == 9 : # digits[i]為9,要進位                 digits[i] = 0             else :        ...

LeetCode 解題練習:Largest Number At Least Twice of Others

題目原文描述  https://leetcode.com/problems/largest-number-at-least-twice-of-others/ 中文描述 給定一個有唯一最大值的整數陣列 nums ,請判斷此最大值元素是否為其他陣列元素值的兩倍以上,若有成立,請回傳此最大值在陣列中的索引位置。 範例一: 輸入 nums = [1, 2, 3, 6, 2, 3, 1] 輸出 3 因為 6 是最大值,且都為其他陣列元素[1, 2, 3]的兩倍以上。 範例二: 輸入 nums = [1, 2, 3, 4, 6] 輸出 -1 因為 6 是最大值,但 6 不是 4 的兩倍以上。 解法一: Two Pass。 先找出最大值 largestNum 與索引位置 largestIdx。 判斷陣列中所有非最大值之元素是否有 largestNum 小於 nums[i] * 2,若有,回傳 -1。 Python Code class Solution :     def dominantIndex ( self , nums : List[ int ]) -> int :         largestNum = - 1 # 最大值         largestIdx = - 1 # 最大值位置                 # 找最大值與索引位置         for i in range ( len (nums)):             if nums[i] > largestNum:                 largestNum = nums[i]                 largestIdx = i           ...

LeetCode 解題:Find Pivot Index

圖片
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much. 題目原文描述  https://leetcode.com/problems/find-pivot-index/ 中文描述 給定一個整數陣列 nums ,找出某目標索引位置 Pivot Index,讓目標索引位置的左邊元素陣列元素總和等於目標索引位置的右邊陣列元素總和,總和不包含目標索引位置之值。 範例一: 輸入 nums = [1, 2, 3, 4, 2, 2, 2]  輸出  3 因為  [1, 2, 3] 總和等於 6 等於 [2, 2, 2]總和 範例二: 輸入 nums = [3, -3, 3]  輸出  0 因為  [] 總和等於 0 等於 [-3, 3] 總和 解法一: 暴力法。對每一個索引位置算出左邊元素陣列元素總和 leftSum 與右邊元素陣列元素總和 rightSum 是否相等。 Python Code class Solution :     def pivotIndex ( self , nums : List[ int ]) -> int :         total = sum (nums) # 陣列總和         leftSum = 0 # 左邊總和         for i in range ( len (nums)): # 從左邊索引0開始找起             rightSum = sum (nums[i+ 1 :]) # 右邊總和             leftSum = total - rightSum - nums[i] # 左邊總和           ...