【題解】ZeroJudge c036: 00573 – The Snail

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c036

#include <iostream>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    double H, U, D;
    double F;
    while (cin >> H >> U >> D >> F && H+D+U+F){
        F /= 100.0;
        double step = U;
        double pos = 0.0;
        int day = 0;
        bool isOut = false;
        while (1){
            day++;
            pos += step;
            if (pos > H){
                isOut = true;
                break;
            }
            pos -= D;
            if (pos < 0.0) break;
            step = max(0.0, step - U * F);
        }
        if (isOut) cout << "success on day " << day << "\n";
        else cout << "failure on day " << day << "\n";
    }
    return 0;
}

Python code (credit: Amy Chou)

while True:
    H, U, D, F = map(int, input().strip().split())
    if H == 0:
        break
    
    F = F / 100
    step = U
    pos = 0
    day = 0
    isOut = False
    while True:
        #print(f"Day-{day}, step={step:.3f}, pos={pos:.3f}")
        day += 1
        pos += step
        if pos > H:
            isOut = True
            break
        pos -= D
        if pos < 0:
            break
        step = max(0, step - U * F)
    
    if isOut:
        print(f"success on day {day}")
    else:
        print(f"failure on day {day}")
分享本文 Share with friends