【深基2.习6】Apples Prologue / 苹果和虫子 · 教师题解

【深基2.习6】Apples Prologue / 苹果和虫子

解题思路

若 t=0,苹果立即全部被吃掉;否则已被动过的苹果数是向上取整 s/t,完整苹果为 max(0,m-该数量)。

正确性说明

不足一个完整周期时,正在吃的苹果也不再完整,因此需要向上取整。时间超过全部吃完所需时间时,剩余只能为 0。

易错点

必须先处理 t=0,防止除零;正在吃的不算完整苹果。

复杂度

时间 O(1),空间 O(1)。

参考代码(C++11)

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int m, t, s;
    cin >> m >> t >> s;
    if (t == 0)
        cout << 0 << "\n";
    else
        cout << max(0, m - (s + t - 1) / t) << "\n";
    return 0;
}
返回题目