【解題】Zerojudge j605. 1. 程式考試
題目連結
j605. 1. 程式考試
我的想法
APCS 第一題純水題
參考解答
1 |
|
j605. 1. 程式考試
APCS 第一題純水題
1 | #include <iostream> |
所謂的自由,就是被別人討厭
j180: 戰備存糧 (Food)
這題比較有趣,也有點像貪婪,涉及到每次食物下次減少時,倉庫的個數也要減少到最佳狀況(把倉庫減少當成守衛減少)。可以直接看我程式碼應該會比較好理解。
舉例
1 | 3 4 |
這筆測資食物總共有 12 個。
第一天減少 3 個(倉庫當前數量;守衛數量):12 - 3 = 9
確認剩下的食物是否可以塞進小於等於當前倉庫數量的大小(每次減少 -1):(3 - 1) * 4 = 8
9 <= 8
所以今天先不減小
第二天減少 3 個(倉庫當前數量;守衛數量):9 - 3 = 6
確認剩下的食物是否可以塞進小於等於當前倉庫數量的大小(每次減少 -1):(3 - 1) * 4 = 8
6 <= 8
所以今天倉庫數量減至 2
第三天減少 2 個(倉庫當前數量;守衛數量):6 - 2 = 4
確認剩下的食物是否可以塞進小於等於當前倉庫數量的大小(每次減少 -1):(2 - 1) * 4 = 4
4 <= 4
所以今天倉庫數量減至 1
最後四天相同做法(也可以判斷數量為 1 時,剩下食物個數加上前面 n 天之後,直接輸出 3 + 4 = 7 天,不過應該不影響答案)
1 | #include <iostream> |
j179: 資料分類 (Classification)
這題用到 #include <string>
裡的 stoi()
函式和 to_string()
函式。(如果你想自己寫出來這兩個函式也可以www)
然後其他照著題目敘述做應該可以吧!
1 | #include <iostream> |
1 | 輸出: |
1 | #include <iostream> |
1 | 輸出: |
1 | #include <iostream> |
j178: 手遊廣告 (Advertisement)
水題。
1 | #include <iostream> |
d732: 二分搜尋法
純純的二分搜尋
範例:找 8
1 | 1 2 3 4 5 6 7 8 9 |
5 < 8
所以左邊往中點 +1 移動
1 | 1 2 3 4 5 6 7 8 9 |
7 < 8
所以左邊往中點 +1 移動
1 | 1 2 3 4 5 6 7 8 9 |
8 = 8
所以結束二分搜
1 | #include <iostream> |
f832: 隕石 (Meteorite)
貪婪演算法(Greedy),每一步都使用最佳的解,從而導致希望結果也是最佳的解。
就範例二而言
1 | 4 4 |
機器之間或隕石之間,又或者是機器與隕石間無任關聯,我們將他們排序
1 | 1 5 7 8 |
當機器可抓取的最大能力是 9,先找到能符合小於 9 的對大數字是 8(此時就是這一步的最佳解),sum += 8
,並且換另一台機器。
當機器可抓取的最大能力是 6,先找到能符合小於 6 的對大數字是 5(此時就是這一步的最佳解),sum += 5
,並且換另一台機器。
當機器可抓取的最大能力是 6,先找到能符合小於 6 的對大數字是 1(此時就是這一步的最佳解),sum += 1
,並且換另一台機器。
沒石頭可以抓,輸出 sum
,結束程式。
:::warning
總和會大於 2^32
,故 sum
要使用 long long
:::
1 | #include <algorithm> |
a290: 新手訓練系列 ~ 圖論
先將圖建好,再用 BFS 慢慢走訪,從訪問的起點開始,走過的地方都標記為 true ,若最後終點是 true,輸出 Yes!!!,否則 No!!!
1 | #include <iostream> |