Official

A - 中古車の選定 / Used Car Selection Editorial by MMNMM Editorial for printing


初心者の方へ

この問題は、for 文などを用いてそれぞれの車に対して条件を満たすかチェックすることで解くことができます。 \(L\le P _ i\le R\) の条件をそのまま記述することができない言語では、\(L\le P _ i\) かつ \(P _ i\le R\) のように判定するのがよいでしょう。

実装例は以下のようになります。

#include <iostream>
using namespace std;

int main() {
    int N, L, R, M;
    cin >> N >> L >> R >> M;
    
    int count = 0; // 候補車の台数を求めるカウンタ
    for (int i = 0; i < N; ++i) {
        int P, K;
        cin >> P >> K;
        if (L <= P && P <= R && K <= M) { // 条件を満たすなら
            ++count; // カウンタを進める
        }
    }
    
    cout << count << endl;
    return 0;
}
N, L, R, M = map(int, input().split())

count = 0 # 候補車の台数を求めるカウンタ
for i in range(N):
    P, K = map(int, input().split())
    if L <= P <= R and K <= M: # 条件を満たすなら
        count += 1 # カウンタを進める

print(count)

posted:
last update: