【APCS】2024年10月實作題 C++ 解題筆記(前兩題)
此筆記僅供個人學習用途,內容僅供參考。
- https://zerojudge.tw/ShowProblem?problemid=o711
題目說明:
該水杯可以被視為上下兩段直立長方體所組成:
下半段:底面積 $w1 \times w1$ ,高 $h1$ 。
上半段:底面積 $w2 \times w2$ ,高 $h2$ 。
倒水時:
水先填滿下半段,接著填上半段。
每次倒入的體積 $v$ ,轉換為水位高度上升量(根據在哪一段來決定用哪段的底面積去除體積,得到水位高度)。
解題思路:
- 先計算兩段的容量(下、上)。
- 記錄目前水位高度。
- 模擬每一次倒入:
- 判斷水還在下半段還是已經進入上半段。
- 根據當前段的底面積換算水位上升高度。
- 水滿之後不再上升。
- 記錄每次倒水所產生的「水位上升高度」,並取最大值輸出。
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
| #include <iostream> #include <vector> using namespace std;
int main() { int n; cin >> n;
int w1, w2, h1, h2; cin >> w1 >> w2 >> h1 >> h2;
vector<int> pours(n); for (int i = 0; i < n; ++i) { cin >> pours[i]; }
int volume1 = w1 * w1 * h1; int volume2 = w2 * w2 * h2; int totalVolume = volume1 + volume2;
int currentVolume = 0; int maxRise = 0;
for (int i = 0; i < n; ++i) { int v = pours[i]; int oldVolume = currentVolume; currentVolume += v; if (currentVolume > totalVolume) { currentVolume = totalVolume; }
int rise = 0;
if (oldVolume < volume1 && currentVolume <= volume1) { rise = (currentVolume - oldVolume) / (w1 * w1); } else if (oldVolume < volume1 && currentVolume > volume1) { int part1 = (volume1 - oldVolume) / (w1 * w1); int part2 = (currentVolume - volume1) / (w2 * w2); rise = part1 + part2; } else { rise = (currentVolume - oldVolume) / (w2 * w2); }
if (rise > maxRise) { maxRise = rise; } }
cout << maxRise; return 0; }
|
- https://zerojudge.tw/ShowProblem?problemid=o712
題目說明:
你有一張 $M \times N$ 的地圖,每一格有一些寶石(或牆壁)。
有一個機器人從某個位置出發,一開始朝右邊,按照這些規則移動:
- 如果格子是 $0$ 顆寶石,機器人會停下來。
- 如果有寶石,加到分數並撿走一顆(格子上的數量 $-1$ )。
- 如果現在的分數是 $k$ 的倍數,機器人就會 右轉 $90$ 度。
- 如果他正前方是 牆壁或邊界外,就繼續右轉,直到找到能走的格子,然後繼續。
最後輸出總共撿了幾顆寶石。
另外:
- 轉彎的時機是撿完寶石且加分數後。
- 要確保走的格子 不是牆壁 (-1) 且 不出界。
解題思路:
- 設二維 vector 變數 grid 存地圖。
- 用一個向量陣列來記錄方向 dx, dy,代表四個方向(右、下、左、上)。
- 機器人邏輯設計:
- 如果當前位置寶石數是 0,停止。
- 否則將寶石數加到 score,並將該格寶石數量減 1。
- 如果 score % k == 0,右轉(方向 +1)。
- 往該方向走,如果遇到牆壁或出界,就持續右轉直到可以走為止。
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
| #include <iostream> #include <vector>
using namespace std;
int main() { int M, N, k, r, c; cin >> M >> N >> k >> r >> c; vector<vector<int>> grid(M, vector<int>(N)); for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) cin >> grid[i][j]; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int dir = 0; int score = 0, gems = 0;
while (true) { if (grid[r][c] == 0) break; score += grid[r][c]; grid[r][c]--; gems++; if (score % k == 0) dir = (dir + 1) % 4; for (int i = 0; i < 4; ++i) { int nr = r + dx[dir]; int nc = c + dy[dir]; if (nr >= 0 && nr < M && nc >= 0 && nc < N && grid[nr][nc] != -1) { r = nr; c = nc; break; } dir = (dir + 1) % 4; } } cout << gems; return 0; }
|