ZeroJudge B138: 陶陶摘苹果

題目敘述

每筆測資第一行有10個正整數,分別代表10個蘋果距離地面的高度 (單位:釐米),第二行有一個正整數,代表陶陶伸手能觸碰到的高度 (單位:釐米),陶陶有一個長30釐米長的凳子。要求輸出陶陶能碰到幾顆蘋果。


範例輸入 #1

100 200 150 140 129 134 167 198 200 111

110

範例輸出 #1

5


解題思路

因為有一個30釐米長的凳子,所以在輸入蘋果高度的時候可以直接-30釐米。之後再一個一個判斷高度是否小於等於手能伸到的高度即可。

解題程式碼如下 (僅供參考):

#include <iostream>
#include <vector>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    vector<int>num;
    for (int i = 0; i<10; i++)
    {
        int tmp;
        cin >> tmp;
        tmp -= 30;
        num.push_back(tmp);
    }
    int height, ans = 0;
    cin >> height;
    for (int i = 0; i<10; i++)
    {
        if (height >= num[i]) ans++;
    }
    cout << ans << "\n";
}

留言

這個網誌中的熱門文章

ZeroJudge M933: 邏輯電路

ZeroJudge A148: You Cannot Pass?!

ZeroJudge A263: 日期差幾天