這題在學什麼
五個號碼與五筆金額按相同索引配對,相同號碼的不同格都要計入。
符合 a 或 b 時金額只加一次;符合 c 時另扣該格金額,並記錄 c 曾出現。
全部處理後,若從未遇到 c 就加倍,最後取與 0 的較大值。
Step-by-step animation
刮刮樂・逐格加減
步驟 1 / 3
先加後扣
號碼 2 加 400,號碼 8 扣 300。
2: +400
8: -300
- total
- 100
- 遇到 c
- 是
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a, b, c;
cin >> a >> b >> c;
vector<int> x(5);
for (int &v : x)
cin >> v;
long long total = 0;
bool bad = false;
for (int v : x) {
long long money;
cin >> money;
if (v == a || v == b)
total += money;
if (v == c) {
total -= money;
bad = true;
}
}
if (!bad)
total *= 2;
cout << max(0LL, total) << '\n';
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。