1 条题解

  • 0
    @ 2044-7-11 2:38:29

    【深基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;
    }
    
    • 1

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

    信息

    ID
    34
    时间
    1000ms
    内存
    128MiB
    难度
    进阶
    标签
    递交数
    1
    已通过
    1
    上传者