online advertising

Wednesday, September 10, 2014

UVa Problem 573 - The Snail

Problem:

Please find the problem here.

Solution:

I might be able to solve the problem analytically - this is most likely just a quadratic relationship - but implementing it as straight forward loop is even easier, and get accepted.

One thing I learn in this exercise is to read the instruction careful, and be careful with boundary conditions, that's all.

Code:

#include "stdafx.h"

// http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=514

#include "UVa573.h"

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int UVa573()
{
    int h, u, d, f;
    while (true)
    {
        cin >> h;   // Height of the well in feet
        cin >> u;   // Climbing height in feet
        cin >> d;   // Sliding height in feet
        cin >> f;   // Percentage fatigue
        if (h == 0)
        {
            break;
        }

        // Run the snail!
        double well_height = h;
        double slide_height = d;
        double strength = u;
        double current_height = 0;
        double fatigue = f / 100.0 * strength;

        int day = 0;
        while (true)
        {
            day++;
            current_height += strength;
            if (current_height > well_height)
            {
                cout << "success on day " << day << endl;
                break;
            }
            current_height -= slide_height;
            if (current_height < 0)
            {
                cout << "failure on day " << day << endl;
                break;
            }

            strength -= fatigue;
            if (strength < 0)
            {
                strength = 0;
            }
        }
    }

    return 0;
}

No comments :

Post a Comment