Friday, May 23, 2014

Topcoder high school SRM 2 250 pt - Level one - Fountain Of Life solution

Topcoder high school SRM 2 250 pt - Level one - Fountain Of Life statement: http://community.topcoder.com/stat?c=problem_statement&pm=6252&rd=10024

Topcoder high school SRM 2 250 pt - Level one - Fountain Of Life editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm2

Topcoder high school SRM 2 250 pt - Level one - Fountain Of Life solution:

#include <iostream>

using namespace std;

class FountainOfLife {
    public: double elixirOfDeath(int elixir, int poison, int pool) {
        if((poison < elixir) || ((poison==elixir) && pool>0)) return -1.0;
        else {
            double a=elixir, b=poison, c=pool;
            return c/(b-a);
        }
    }
};

note: if elixir is greater than poison, fountain will never become deadly, so we return -1.0 . Otherwise, if elixir is smaller than poison then time taken to fountain to become deadly is closing the gap(difference) between elixir and poison, and that gap is pool, so we just return pool/(poison - elixir);

No comments:

Post a Comment