發表文章

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

圖片
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or buy me a coffee . Thank you very much. 題目連結 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 ;...

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] # 左邊總和           ...

LeetCode 解題練習:Find All Numbers Disappeared in an Array

圖片
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 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-all-numbers-disappeared-in-an-array/ 中文描述 給定一個含有[1, n] 之間的n個元素之整數陣列 nums (陣列長度為 n),找出[1, n] 之間沒有出現在 陣列 nums 元素中的整數。  範例一: 輸入 nums = [2, 2]  輸出 [1] 範例二: 輸入 nums = [1, 2, 2]  輸出 [3] 範例三: 輸入 nums = [1, 2, 1, 2, 1, 3, 4]  輸出 [5, 6, 7] 解法一: 使用計數的方式記錄[1,n]每一個數字出現在 nums 的次數,再統計出現次數為0的數字有哪些。 Python Code class Solution :     def findDisappearedNumbers ( self , nums : List[ int ]) -> List[ int ]:         n = len (nums)         counts = [ 0 ] * n # 計算 [1,n] 數字出現的次數                 for i in nums:             counts[i - 1 ] += 1 # 出現一次就增加1                 missingNumbers = [] # 沒出現在 nums 的數字             ...

LeetCode 解題練習:Height Checker

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 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/height-checker/   中文描述 給定一個整數陣列 heights ,請算出 heights 中沒有依照由小到大之順序的元素個數。 範例一: 輸入 heights = [3, 1, 2]  輸出 3 因為依照小到大的陣列為 [1, 2, 3] ,heights 每個元素皆不再所屬的排序位置上。 範例二: 輸入 heights = [1, 3, 2]  輸出 2 因為依照小到大的陣列為 [1, 2, 3] ,heights[0]有在排序的位置上,其餘元素皆不在所屬的排序位置上。 範例三: 輸入 heights = [1, 2, 3]  輸出 0 因為依照小到大的陣列為 [1, 2, 3] ,heights所有元素皆在所屬的排序位置上。 解法: 先用內建排序函式產生由小排到大的 expected 陣列,依照索引位置 i 算出 heights[i] != expected[i] 有多少個。 Python Code class Solution :     def heightChecker ( self , heights : List[ int ]) -> int :         expected = sorted (heights)         notMatch = 0         for i in range ( len (heights)):             if heights[i] != expected[i]:                 not...